我正在使用zend gdata库编写一个2 Legged OAuth应用(Google Marketplace应用)。 我需要获取谷歌联系人。
以下给出的代码。
require_once 'Zend/Oauth/Consumer.php';
$oauthOptions = array(
'requestScheme' => Zend_Oauth::REQUEST_SCHEME_HEADER,
'version' => '1.0',
'signatureMethod' => 'HMAC-SHA1',
'consumerKey' => $CONSUMER_KEY,
'consumerSecret' => $CONSUMER_SECRET
);
$consumer = new Zend_Oauth_Consumer($oauthOptions);
$token = new Zend_Oauth_Token_Access();
$httpClient = $token->getHttpClient($oauthOptions);
$url = 'http://www.google.com/m8/feeds/contacts/default/full';
require_once 'Zend/Gdata/Gapps.php';
$gdata = new Zend_Gdata($httpClient);
require_once 'Zend/Gdata/Query.php';
require_once 'Zend/Gdata/Feed.php';
require_once 'Zend/Gdata/App.php';
$query = new Zend_Gdata_Query($url);
try {
$feed = $gdata->getFeed($query);
} catch(Zend_Gdata_App_Exception $ex){
print_r($ex->getMessage());
}
我收到以下错误:
Expected response code 200, got 401
Unknown authorization header
Error 401
执行上述代码时发送的HTTP标头是:
GET /m8/feeds/contacts/default/full HTTP/1.1
Host: www.google.com
Connection: close
User-Agent: MyCompany-MyApp-1.0 Zend_Framework_Gdata/1.11.0dev
Accept-encoding: identity
Authorization: OAuth realm="",oauth_consumer_key="686518909188.apps.googleusercontent.com",oauth_nonce="fc99e10f42cdb01c7f3ce1ab2775e616",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1316583946",oauth_version="1.0",oauth_signature="hlbTvPExy4r4%2FyY1ddEsy1AJhf4%3D"
收到HTTP响应:
HTTP/1.1 401 Unknown authorization header
Content-Type: text/html; charset=UTF-8
Date: Wed, 21 Sep 2011 05:45:47 GMT
Expires: Wed, 21 Sep 2011 05:45:47 GMT
Cache-Control: private, max-age=0
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Server: GSE
Connection: close
<HTML>
<HEAD>
<TITLE>Unknown authorization header</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Unknown authorization header</H1>
<H2>Error 401</H2>
</BODY>
</HTML>
请让我知道我在这里失踪了什么。
答案 0 :(得分:2)
使用2脚OAuth(xoauth)时,似乎未设置“xoauth_requestor_id”请求参数。
我尝试在传递给Zend_Gdata_Calendar构造函数的HttpClient对象上设置此查询参数:
$httpClient->setParameterGet('xoauth_requestor_id', $userEmail);
但不幸的是,这种干净的方法不起作用。似乎Zend_GData_Calendar类没有完全利用注入的HttpClient对象。
所以我使用扩展Zend_GData_Calendar并实例化我的自定义版本。我还没有完全测试所有的功能,但这是我到目前为止所能提供的内容,它可以轻松构建:
/**
* Slightly modified class which extends Zend_Gdata_Calendar. Fixes a problem with the request URI when using 2-legged
* OAuth (xoauth). Adds on the xoauth_requestor_id query parameter.
*/
class Xoauth_Gdata_Calendar extends Zend_Gdata_Calendar {
protected $requestorId;
/**
* Set the xoauth_requestor_id for requests
*
* @param string $requestorId
*/
public function setRequestorId($requestorId) {
$this->requestorId = $requestorId;
}
/**
* Get the currently set xoauth_requestor_id
*
* @return string
*/
public function getRequestorId() {
return $this->requestorId;
}
/**
* {@inheritdoc} extended to also include the xoauth_requestor_id query parameter in the request URI. This fixes
* an authentication issue when using 2-legged OAuth (xoauth).
*
* @return string|Zend_Gdata_App_Feed
*/
public function getCalendarListFeed() {
$uri = self::CALENDAR_FEED_URI . '/default';
if($this->requestorId) {
$uri .= '?xoauth_requestor_id=' . urlencode($this->requestorId);
}
return parent::getFeed($uri,'Zend_Gdata_Calendar_ListFeed');
}
}
然后使用它来实例化这个类而不是Zend_Gdata_Calendar基类。您只需要调用setRequestorId($ usersEmail),例如:
$calendarClient = new Xoauth_Gdata_Calendar($httpClient);
$calendarClient->setRequestorId($userEmail);
答案 1 :(得分:0)
我在尝试访问日历时遇到了同样的问题,令人沮丧。我正在使用Zen(1.11.10)的最新下载。
require_once 'Zend/Oauth/Consumer.php';
require_once 'Zend/Gdata/Calendar.php';
$CONSUMER_KEY = 'mydomain.com';
$CONSUMER_SECRET = 'mysecret';
$USER = 'user@mydomain.com';
$oauthOptions = array(
'requestScheme' => Zend_Oauth::REQUEST_SCHEME_HEADER,
'version' => '1.0',
'signatureMethod' => 'HMAC-SHA1',
'consumerKey' => $CONSUMER_KEY,
'consumerSecret' => $CONSUMER_SECRET
);
$consumer = new Zend_Oauth_Consumer($oauthOptions);
$token = new Zend_Oauth_Token_Access();
$httpClient = $token->getHttpClient($oauthOptions);
// Create an instance of the Calendar service
$service = new Zend_Gdata_Calendar($httpClient);
try {
$listFeed= $service->getCalendarListFeed();
} catch (Zend_Gdata_App_Exception $e) {
echo "Error: " . $e->getMessage();
}
答案 2 :(得分:0)
我已就此问题向Zend提交了一份错误报告。见ZF-11880。请投票支持此错误,尝试提高其优先级。
答案 3 :(得分:0)
以下清洁方法无法正常工作
$httpClient->setParameterGet('xoauth_requestor_id', $userEmail);
在Zend / Gdata / App.php的第638行,它执行以下操作
// Make sure the HTTP client object is 'clean' before making a request
// In addition to standard headers to reset via resetParameters(),
// also reset the Slug and If-Match headers
$this->_httpClient->resetParameters();
因为每次请求都会调用您在客户端中设置的内容。
答案 4 :(得分:0)
Xoauth_Gdata_Calendar
修复有效
如果您想发布活动,则还需要覆盖insertEvent()
功能。
/**
* {@inheritdoc} extended to also include the xoauth_requestor_id query
* parameter in the request URI. This fixes an authentication issue
* when using 2-legged OAuth (xoauth).
*
* @return string|Zend_Gdata_Calendar_EventEntry
*/
public function insertEvent($event, $uri=null) {
if ($uri == null) {
$uri = $this->_defaultPostUri;
if($this->requestorId) {
$uri .= '?xoauth_requestor_id=' . urlencode($this->requestorId);
}
}
return parent::insertEntry($event, $uri, 'Zend_Gdata_Calendar_EventEntry');
}