【发布时间】:2015-01-06 07:00:15
【问题描述】:
我整天都在处理这个问题,但我就是不知道出了什么问题。
我有一个使用适用于 javascript 的 Google Api 客户端的应用程序,它可以正常工作。现在我想在服务器端做点什么,所以研究了一下,发现要走的路是在客户端使用令牌,在后端使用setAccessToken方法。
所以我尝试将我的令牌对象作为 JSON 发送(使用 JSON.stringify(gapi.auth.getToken()) ),一旦我尝试在需要身份验证的后端进行 API 调用,就会收到以下错误:
OAuth 2.0 访问令牌已过期,刷新令牌不可用。自动批准的响应不会返回刷新令牌。
所以,有点困惑,我尝试在谷歌的端点上使用 curl 来验证令牌,它返回以下内容
{
"issued_to": "client_id",
"audience": "client_id",
"user_id": "user_id",
"scope": "https://www.googleapis.com/auth/youtube.readonly https://www.googleapis.com/auth/youtube.upload https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/plus.me",
"expires_in": 1465,
"email": "user_email",
"verified_email": true,
"access_type": "online"
}
所以我知道令牌很好且有效。我的代码设置方式如下(编辑为:
<?php
// The token JSON is not inline, it comes from another source directly from the client side, but this is how it looks
$token_json = '{"state":"","access_token":"TOTALLY_VALID_ACCESS_TOKEN","token_type":"Bearer","expires_in":"3600","scope":"https://www.googleapis.com/auth/youtube.readonly https://www.googleapis.com/auth/youtube.upload https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/plus.me","client_id":"CLIENT_ID","g_user_cookie_policy":"single_host_origin","cookie_policy":"single_host_origin","response_type":"token","issued_at":"1415583001","expires_at":"1415586601","g-oauth-window":{},"status":{"google_logged_in":false,"signed_in":true,"method":"PROMPT"}}';
$OAUTH2_CLIENT_ID = 'CLIENT_ID';
$OAUTH2_CLIENT_SECRET = 'CLIENT_SECRET';
$client = new Google_Client();
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$client->setAccessToken($token_json);
$youtube = new Google_Service_YouTube($client);
try{
/* Random code goes here */
// Auth Exception here.
$insertRequest = $youtube->videos->insert("status,snippet", $video);
} catch (Google_Exception $e) {
var_dump($e->getMessage());
}
我需要设置离线访问吗?我要求登录过程必须在 javascript 中,所以没有机会从后端重新创建登录流程。
我有什么遗漏的吗?
【问题讨论】:
标签: javascript php oauth google-api-php-client