我正在使用 gdata 使用 Contact API 将联系人添加到我的 gmail 帐户。当我在我的开发框上以 localhost 运行代码时,一切正常。但是,当我将代码移动到生产服务器 (www.somedomain.com) 时,我收到“错误:Google 身份验证失败。原因:BadAuthentication”。
这是我的代码:
<?php
// load Zend Gdata libraries
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Http_Client');
Zend_Loader::loadClass('Zend_Gdata_Query');
Zend_Loader::loadClass('Zend_Gdata_Feed');
// set credentials for ClientLogin authentication
$user = "something@gmail.com";
$pass = "somepassword";
try {
// perform login and set protocol version to 3.0
$client = Zend_Gdata_ClientLogin::getHttpClient(
$user, $pass, 'cp');
$gdata = new Zend_Gdata($client);
$gdata->setMajorProtocolVersion(3);
// create new entry
$doc = new DOMDocument();
$doc->formatOutput = true;
$entry = $doc->createElement('atom:entry');
$entry->setAttributeNS('http://www.w3.org/2000/xmlns/' ,
'xmlns:atom', 'http://www.w3.org/2005/Atom');
$entry->setAttributeNS('http://www.w3.org/2000/xmlns/' ,
'xmlns:gd', 'http://schemas.google.com/g/2005');
$entry->setAttribute('xmlns', 'http://www.w3.org/2005/Atom');
$entry->setAttribute('xmlns:gd', 'http://schemas.google.com/g/2005');
$entry->setAttribute('xmlns:gContact', 'http://schemas.google.com/contact/2008');
$doc->appendChild($entry);
// add name element
$name = $doc->createElement('gd:name');
$entry->appendChild($name);
$fullName = $doc->createElement('gd:fullName', 'Jack Frost');
$name->appendChild($fullName);
// add email element
$email = $doc->createElement('gd:email');
$email->setAttribute('address' ,'jack.frost@example.com');
$email->setAttribute('rel' ,'http://schemas.google.com/g/2005#home');
$entry->appendChild($email);
// add org name element
$org = $doc->createElement('gd:organization');
$org->setAttribute('rel' ,'http://schemas.google.com/g/2005#work');
$entry->appendChild($org);
$orgName = $doc->createElement('gd:orgName', 'Winter Inc.');
$org->appendChild($orgName);
//add to Friends list
$group = $doc->createElement('gContact:groupMembershipInfo');
$group->setAttribute('deleted' ,'false');
$group->setAttribute('href' ,'http://www.google.com/m8/feeds/groups/' .urlencode($user) . '/base/[groupid]');
//addd to my contacts
$entry->appendChild($group);
$group = $doc->createElement('gContact:groupMembershipInfo');
$group->setAttribute('deleted' ,'false');
$group->setAttribute('href' ,'http://www.google.com/m8/feeds/groups/' .urlencode($user) . '/base/6'); //adds to Friends
$entry->appendChild($group);
// insert entry
$entryResult = $gdata->insertEntry($doc->saveXML(),
'http://www.google.com/m8/feeds/contacts/default/full');
echo '<h2>Add Contact</h2>';
echo 'The ID of the new entry is: ' . $entryResult->id;
echo 'The link is: <a href="'.$entryResult->getLink('edit').">".$entryResult->getLink('edit')."</a>";
} catch (Exception $e) {
die('ERROR:' . $e->getMessage());
}
?>
我应该改用 OAuth for Services 吗?我无法找到有关如何使用 Zend Gdata 执行此操作的文档。
编辑 - 找到答案:
好的 - 我仍然对更好的身份验证方法有疑问,但我知道在这种情况下问题出在哪里。Google 怀疑我的应用程序登录。当我检查 gmail 时,我收到了以下信息:
Hi Info,
Someone recently used your password to try to sign in to your Google Account [someone]@gmail.com. This person was using an application such as an email client or mobile device.
We prevented the sign-in attempt in case this was a hijacker trying to access your account. Please review the details of the sign-in attempt:
Saturday, November 2, 2013 9:43:52 PM UTC
IP Address: [xxx.xxx.xx.xxx] ([xxx.xxx.xx.xxx].unifiedlayer.com.)
Location: Unknown
If you do not recognize this sign-in attempt, someone else might be trying to access your account. You should sign in to your account and reset your password immediately.
Reset password
If this was you, and you are having trouble accessing your account, complete the troubleshooting steps listed at http://support.google.com/mail?p=client_login
Sincerely,
The Google Accounts team
我进入了 gmail 安全性(通过提供的链接)并能够授权该应用程序。问题解决了。现在完美运行。
我仍然想要一些关于如何在 Zend 中使用我的客户 ID、服务帐户和私钥的指示(如果可能的话)。我已经使用 Google API php 库进行了连接,但我看不到如何在 Zend 框架中使用它。