我意识到这些帖子中大约有20个,但我已经查看了所有这些帖子,到目前为止没有任何答案有帮助(虽然帮助我排除了事情)。
我在PHP中编写了一堆与Youtube相关的api函数,一旦我有访问代码(也没有刷新访问代码的问题),我就没有问题,但是当我尝试交换授权代码时对于访问令牌,我收到“invalid_grant”响应。
我还需要离线访问,因为我计划在没有用户交互的情况下进行api调用,并且不能预先授权帐户。
access.php
$ytclient = new ytClient;
if(isset($_GET['code'])){
$ytclient->auth_code = $_GET['code'];
$ytclient->exchangeToken();
}else{
$ytclient->authorize();
}
ytClient
function authorize(){
include('config.php');
echo "<script>window.location = 'https://accounts.google.com/o/oauth2/auth?redirect_uri=".urlencode( $config->domain.'/lib/access.php')."&response_type=code&client_id=**************&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyt-analytics-monetary.readonly+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyt-analytics.readonly+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube.readonly+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube.upload+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutubepartner&approval_prompt=force&access_type=offline'</script>";
}
function exchangeToken(){
$postArr["client_id"] = ($config->ytclientid);
$postArr["client_secret"] = ($config->ytclientsecret);
$postArr["code"] = urlencode($this->auth_code);
$postArr["request_uri"] = urlencode($config->domain."/lib/access.php");
$postArr["grant_type"] = "authorization_code";
$accessObj = $this->get_yt_json("https://accounts.google.com/o/oauth2/token", $postArr, 1);
}
function get_yt_json($url, $postArr, $mode){
if($mode == 1){
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postArr);
}elseif($mode == 2){
$url .= "?".http_build_query($postArr);
$curl = curl_init($url);
}
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$rawJson = curl_exec($curl);
curl_close($curl);
echo $rawJson;
/*
$obj = json_decode($rawJson);
return ($obj);
*/
}
响应
HTTP/1.1 400 Bad Request
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: Fri, 01 Jan 1990 00:00:00 GMT
Date: Sat, 21 Sep 2013 18:58:31 GMT
Content-Type: application/json
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Server: GSE
Alternate-Protocol: 443:quic
Transfer-Encoding: chunked
{ "error" : "invalid_grant" }
答案 0 :(得分:1)
从未找到解决方案,但使用高度无证的google php api =&gt;修复了它。 https://code.google.com/p/google-api-php-client/
快速简便的解决方案 - 据我所知,魔术......以下是使用api对使用终身令牌验证某人的代码,可能有助于某人:
include(dirname(__FILE__).'/google-api-php-client/src/Google_Client.php');
$client = new Google_Client();
$client->setApprovalPrompt('force');
$client->setAccessType('offline');
$client->setScopes('https://www.googleapis.com/auth/yt-analytics-monetary.readonly https://www.googleapis.com/auth/yt-analytics.readonly https://www.googleapis.com/auth/youtube https://www.googleapis.com/auth/youtube.readonly https://www.googleapis.com/auth/youtube.upload https://www.googleapis.com/auth/youtubepartner');
/* Auth the client and get Token Object */
$auth = $client->authenticate();
$token = json_decode($client->getAccessToken());