OAuth认证后LinkedIn API调用的问题


Issue with LinkedIn API call after OAuth authentication

我已经成功地完成了LinkedIn OAuth过程(使用REST API - OAuth 1.0a)。然而,我在回调后的第一个API调用遇到了麻烦。我在我正在编写的库中设置UserToken, UserTokenSecret和UserVerfier,并调用此函数以获取我的配置文件信息:

public function getUserProfile()
{
    $consumer = new OAuthConsumer($this->consumer_key, $this->consumer_secret, NULL);
    $auth_token = new OAuthConsumer($this->getUserToken(), $this->getUserTokenSecret());
    $access_token_req = new OAuthRequest("GET", $this->access_token_endpoint);
    $params['oauth_verifier'] = $this->getUserVerifier();
    $access_token_req = $access_token_req->from_consumer_and_token($this->consumer,
            $auth_token, "GET", $this->access_token_endpoint, $params);
    $access_token_req->sign_request(new OAuthSignatureMethod_HMAC_SHA1(),$consumer,
            $auth_token);
    $after_access_request = $this->doHttpRequest($access_token_req->to_url());
    $access_tokens = array();
    parse_str($after_access_request,$access_tokens);
    # line 234 below
    $access_token = new OAuthConsumer($access_tokens['oauth_token'], $access_tokens['oauth_token_secret']);

    // prepare for get profile call
    $profile_req = $access_token_req->from_consumer_and_token($consumer,
            $access_token, "GET", $this->api_url.'/v1/people/~');
    $profile_req->sign_request(new OAuthSignatureMethod_HMAC_SHA1(),$consumer,$access_token);
    $after_request = $this->doHttpRequest($profile_req->to_url());

    var_dump($after_request);
}

函数var_dump一个字符串,这是我的配置文件的基本概要:

string(402) " User Name etc. etc. http://www.linkedin.com/profile?viewProfile=&key=28141694&authToken=HWBC&authType=name&trk=api*a137731*s146100* "

很好。然而,当我刷新页面时,相同的函数调用失败了:

Undefined index: oauth_token, line number: 234 

(上面代码块中标记有注释的这一行)。

然后,当然,var_dump从LinkedIn报告这个错误:

string(290) " 401 1310652477038 R8MHA2787T 0 [unauthorized]. The token used in the OAuth request is not valid. "

注意事项:

  • 用户令牌、密钥和验证器在初始授权回调期间(就在调用此函数之前)被持久化。因此,它们在第一次调用期间(当它工作时,从linkedin回来后)和页面重新加载期间(当它在第234行失败时)是相同的。

而且,我必须承认我不能100%确定我理解这个函数中发生的一切。实际上,我从本教程(关于不同的服务,而不是linkedin) http://apiwiki.justin.tv/mediawiki/index.php/OAuth_PHP_Tutorial中获取了一些示例,并将其与我从linkedin API文档中收集到的信息结合起来,并在他们的开发人员站点上传播。最值得注意的是教程中没有使用的"验证者"。

对于这个问题的任何见解都将是非常感谢的。提前感谢。尼克<标题> 更新

我唯一能做到的就是每次都进行一次新的OAuth握手。事情就应该这样发生吗?我的印象是,一旦我获得了我的用户令牌/密钥和验证器,我就可以使用它们进行连续的API调用,直到令牌过期或被撤销。

就像现在一样,每次页面重新加载时,我都会请求一个新的用户令牌、密钥和验证器,然后立即调用以获取用户配置文件(成功)。下一次重新加载,我得到一个全新的密钥/密钥和验证器。似乎每次调用都要做很多工作,据我所知,您应该能够使用此方法执行脱机操作-如果每次都需要新的授权,那么我猜我不能这样做?

好吧。我终于弄明白是怎么回事了,所以我想把答案贴在这里,以防别人遇到这种情况。

我用来作为指导的例子是有缺陷的。在获取访问令牌之后,您应该创建一个新的OAuthRequest对象,而不是使用现有的$access_token_req实例。

:

// prepare for get profile call
$profile_req = $access_token_req->from_consumer_and_token($consumer,
        $access_token, "GET", $this->api_url.'/v1/people/~');
$profile_req->sign_request(new OAuthSignatureMethod_HMAC_SHA1(),$consumer,$access_token);
$after_request = $this->doHttpRequest($profile_req->to_url());

应该改成:

$api_req = new OAuthRequest("GET", $this->api_url.$api_call);
// prepare for get profile call
$api_req = $api_req->from_consumer_and_token($consumer,
        $access_token, "GET", $this->api_url.'/v1/people/~');
$api_req->sign_request(new OAuthSignatureMethod_HMAC_SHA1(),$consumer,$access_token);
$after_request = $this->doHttpRequest($api_req->to_url());