2

Contact API を使用して、gdata を使用して連絡先を Gmail アカウントに追加しています。開発ボックスで localhost としてコードを実行すると、すべて正常に動作します。しかし、コードを本番サーバー (www.somedomain.com) に移動すると、「ERROR:Authentication with Google failed. Reason: 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 のセキュリティにアクセスし、アプリを承認することができました。問題が解決しました。今は完璧に動作します。

クライアント ID、サービス アカウント、およびプライベート キーを Zend で使用する方法についての指針が必要です (可能であれば)。Google API php ライブラリを使用して接続しましたが、Zend フレームワーク内でこれを使用する方法がわかりません。

4

1 に答える 1

0

私の側では、完全なメール アドレスをユーザー名 (username@gmail.com) として入力することで問題を解決しました。

以前、ユーザー名だけで自分のアカウントにアクセスしようとしました...

于 2014-07-27T11:24:44.303 に答える