2013-11-03 39 views
2

我正在使用gdata將聯繫人添加到使用聯繫人API的我的Gmail帳戶。當我作爲localhost在我的開發框上運行代碼時,一切正常。但是,當我將代碼移至生產服務器(www.somedomain.com)時,我收到「錯誤:Google驗證失敗,原因:BadAuthentication」。Zend的Google API BadAuthentication錯誤gdata

這裏是我的代碼:

<?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 = "[email protected]"; 
$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' ,'[email protected]'); 
    $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的服務呢?我一直無法找到關於如何與Zend Gdata做到這一點的文檔。

編輯 - 答案找到:

好了 - 我仍然有一個更好的權威性方法的問題,但我想通了什麼問題是在這種情況下。 。谷歌是我的可疑應用程序登錄的。當我檢查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框架中使用它。

+0

使用Zend登錄YouTube時,今天發生了同樣的事情。感謝您發佈解決方案! –

+0

Google將我的活動標記爲我自己的活動,但仍然拒絕使用「BadAuthentication」消息來訪問我的應用。人力資源管理。 – Dutchie432

回答

0

在我身邊,我通過輸入完整的電子郵件地址作爲用戶名([email protected])來解決問題。

此前,我試圖只使用用戶名訪問我的帳戶...