Google YouTube API刷新令牌未被发送

时间:2012-03-26 15:23:04

标签: php youtube google-api youtube-api oauth-2.0

我正在尝试使用基于Google文档的Google YouTube数据API:https://developers.google.com/youtube/2.0/developers_guide_protocol_oauth2#OAuth2_Refreshing_a_Token。在使用OAuth进行身份验证时,我的问题就出现了。我正在使用以下授权URL,该URL与文档所说的相同,除了我的重定向uri和应用程序密钥,显然。

$this->authorizationUrl = 'https://accounts.google.com/o/oauth2/auth?';
$this->authorizationUrl .= 'client_id=' . $this->applicationKey . '&';
$this->authorizationUrl .= 'redirect_uri=' . $this->redirect_uri . '/account.php?action=youtube_oauth&';
$this->authorizationUrl .= 'scope=https://gdata.youtube.com&';
$this->authorizationUrl .= 'response_type=code&';
$this->authorizationUrl .= 'access_type=offline';

然后,正如文档所说,我提出以下内容:

$curl_options = Array(
            CURLOPT_POSTFIELDS => Array(
                'code' => $code,
                'client_id' => $this->applicationKey,
                'client_secret' => $this->applicationSecret,
                'redirect_uri' => $this->redirect_uri . '/account.php?action=youtube_oauth',
                'grant_type' => 'authorization_code'
            ),
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_URL => 'https://accounts.google.com/o/oauth2/token'
        );

然而,我的回复从来没有像我们的文档所说的那样给我一个refresh_token。我刚收到其他三个回复项目。

像这样的一些问题:Get refresh token google api曾说过使用approval_prompt = force,但这也不起作用,完全违背了access_type = offline的目的。

为什么我会在4个响应项中有3个获得有效回复?

2 个答案:

答案 0 :(得分:29)

来自OAuth2.0文档的offline access部分:

  

当您的应用程序收到刷新令牌时,它就是   存储该刷新令牌以供将来使用很重要。如果你的   应用程序丢失刷新令牌,它将不得不重新提示   用户在获得另一个刷新令牌之前获得同意如果你需要   要重新提示用户同意,请添加approval_prompt   授权代码请求中的参数,并将值设置为   force

因此,当您已授予访问权限后,grant_type authorization_code refresh_token的后续请求将不会返回access_type,即使offline设置为{{1}在同意页面的查询字符串中。

如上面引用中所述,为了在收到 refresh_token之后,您需要通过提示将您的用户发送回来,您可以通过将approval_prompt设置为force

干杯,

PS此更改也在blog post中公布。

答案 1 :(得分:2)

您可以尝试谷歌oauth2游乐场(https://code.google.com/oauthplayground/),看看您的参数之间的区别。