“未知的授权标头”使用 php、(zend) gdata 和 2legged oauth 获取 google 联系人时

发布于 2024-12-05 14:20:44 字数 2239 浏览 1 评论 0原文

我正在使用 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>


请让我知道我在这里缺少什么。

I am writing a 2 Legged OAuth app (Google Marketplace app) using zend gdata library.
I need to fetch google contacts.

Code as given below.

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());
}

I get following error:

Expected response code 200, got 401
Unknown authorization header
Error 401

HTTP headers sent on executing above code are:

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 Response received:

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>

Please let me know what am I missing here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

安静 2024-12-12 14:20:44

使用 2-legged 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);

It appears that the "xoauth_requestor_id" request parameter is not being set when using 2-legged OAuth (xoauth).

I tried setting this query parameter on the HttpClient object that is passed to the Zend_Gdata_Calendar constructor:

$httpClient->setParameterGet('xoauth_requestor_id', $userEmail);

But unfortunately this clean approach didn't work. Seems that the Zend_GData_Calendar class isn't fully utilising the injected HttpClient object.

So I've resorted to extending Zend_GData_Calendar and instantiating my customised version of it instead. I've not fully tested all the functionality but here is what I have so far and it can easily be built upon:

/**
 * 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');
    }
}

Then to use this you instantiate this class rather Zend_Gdata_Calendar base class. You just need to call setRequestorId($usersEmail), for example:

$calendarClient = new Xoauth_Gdata_Calendar($httpClient);
$calendarClient->setRequestorId($userEmail);
若言繁花未落 2024-12-12 14:20:44

我在尝试访问日历时遇到了同样的问题,非常令人沮丧。我正在使用从 Zen (1.11.10) 下载的最新版本。

require_once 'Zend/Oauth/Consumer.php';
    require_once 'Zend/Gdata/Calendar.php';

    $CONSUMER_KEY = 'mydomain.com';
    $CONSUMER_SECRET = 'mysecret';
    $USER = '[email protected]';

    $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();
    }

I ran into the same problem when trying to access the Calendar, so frustrating. I am using the latest download from Zen (1.11.10).

require_once 'Zend/Oauth/Consumer.php';
    require_once 'Zend/Gdata/Calendar.php';

    $CONSUMER_KEY = 'mydomain.com';
    $CONSUMER_SECRET = 'mysecret';
    $USER = '[email protected]';

    $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();
    }
你不是我要的菜∠ 2024-12-12 14:20:44

我已就此事向 Zend 提交了错误报告。请参阅 ZF-11880。请为此错误投票,以尝试提高其优先级。

I have submitted a bug report to Zend on this matter. See ZF-11880. Please vote for this bug to try and get its priority raised.

梦初启 2024-12-12 14:20:44

以下干净方法不起作用是有原因的,

$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();

因为每个请求都会调用此方法,您在客户端中设置的内容将被删除。

There is a reason the following clean approach doesn't work

$httpClient->setParameterGet('xoauth_requestor_id', $userEmail);

On Line 638 of Zend/Gdata/App.php it does the following

// 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();

Because this is called for every request what you set in the client is erased.

誰認得朕 2024-12-12 14:20:44

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');
}

The Xoauth_Gdata_Calendar fix works.
And if you want to post an event, you'll need to override the insertEvent() function too.

/**
 * {@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');
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文