问题
我遇到的错误是invalid_client
和invalid_grant
。我正在使用Oauthplayground的访问令牌。选择&授权API示波器,我还填充了配置选项,以使用我自己的客户端ID和秘密,该选项必须是类型Web Application
,因为Installed/Native Application
和Service Account
投掷mismatch_redirect_uri
错误,并且您不能将重定向尿液添加到Console中的这些类型。
我喜欢我读过的许多其他问题的帖子,需要运行cron作业才能使用PHP客户端curl Google的API。如果您可以在网络/灯环境中准确解释如何使用安装或为此目的使用服务帐户,那么我都是Ears
空curl dump:(从Google_curlio.php的第110行倾倒)
$opts = array();
$opts['headers'] = $requestHeaders;
$opts['data'] = $respData;
$opts['curl'] = curl_getinfo($ch);
$opts['curlversion'] = curl_version();
$opts['curlerror'] = curl_errno($ch);
$opts["params"] = $parsed;
echo(json_encode($opts));
curl_close($ch);
die(); // THIS DUMP RETURNS...
{
headers: {
content-type: "application/x-www-form-urlencoded",
content-length: 209
},
data: false,
curl: {
url: "https://accounts.google.com/o/oauth2/token",
content_type: null,
http_code: 0,
header_size: 0,
request_size: 0,
filetime: -1,
ssl_verify_result: 0,
redirect_count: 0,
total_time: 0.156,
namelookup_time: 0,
connect_time: 0.078,
pretransfer_time: 0,
size_upload: 0,
size_download: 0,
speed_download: 0,
speed_upload: 0,
download_content_length: -1,
upload_content_length: -1,
starttransfer_time: 0,
redirect_time: 0,
certinfo: [ ],
primary_ip: "74.125.21.84",
primary_port: 443,
local_ip: "192.168.0.108",
local_port: 64342,
redirect_url: ""
},
curlversion: {
version_number: 466432,
age: 3,
features: 3005,
ssl_version_number: 0,
version: "7.30.0",
host: "i386-pc-win32",
ssl_version: "OpenSSL/0.9.8y",
libz_version: "1.2.7",
protocols: [
"dict",
"file",
"ftp",
"ftps",
"gopher",
"http",
"https",
"imap",
"imaps",
"ldap",
"pop3",
"pop3s",
"rtsp",
"scp",
"sftp",
"smtp",
"smtps",
"telnet",
"tftp"
]
},
curlerror: 60,
params: [
"content-type: application/x-www-form-urlencoded",
"content-length: 209"
]
}
php代码到OAuth2&做一个简单的报告
require APPPATH . 'third_party/google-api-php-client/src/Google_Client.php';
require APPPATH . 'third_party/google-api-php-client/src/contrib/Google_YouTubeAnalyticsService.php';
$client = new Google_Client();
$client->setApplicationName($settings->ApplicationName);
$client->setRedirectUri('urn:ietf:wg:oauth:2.0:oob'); // http://localhost
//WEB APPLICATION
$settings->ClientId = '1098485864880-xxx.apps.googleusercontent.com';
$settings->AssertionCredentialsEmail = '1098485864880-xxx@developer.gserviceaccount.com';
$settings->clientSecret = '123456789';
//NATIVE APP
//$settings->ClientId = '1098485864880-xxx.apps.googleusercontent.com';
//$settings->AssertionCredentialsEmail = 'my.apis.email@gmail.com';
//$settings->clientSecret = '123456789';
//SERVICE APP
//$settings->ClientId = '1098485864880-xxx.apps.googleusercontent.com';
//$settings->AssertionCredentialsEmail = '1098485864880-xxx@developer.gserviceaccount.com';
//$settings->clientSecret = 'notasecret';
//$settings->AssertionCredentialsKeyFile = 'b175b0ae0f0cf40b43a25f9bce419ec2bc3740ff-privatekey.p12';
//$settings->privateKey = file_get_contents(APPPATH .'third_party/google-api-php-client/cert/'.$settings->AssertionCredentialsKeyFile);
//$settings->privateKeyPassword = 'notasecret';
//$client->refreshToken("1/xxx-yyy");
$client->setAccessToken('{
"access_token" : "ya29.1.xxx",
"token_type" : "Bearer",
"expires_in" : 3600,
"created" : 1, // this is NOT actually provided by the oauthplayground
"refresh_token" : "1/xxx-yyy"
}');
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'], FILTER_SANITIZE_URL);
$settings->assertionType = 'http://oauth.net/grant_type/jwt/1.0/bearer';
$client->setRedirectUri($redirect);
$client->setClientId($settings->ClientId);
$client->setScopes($settings->AssertionCredentialsPerms);
//$client->setAssertionCredentials(new Google_AssertionCredentials(
//$settings->ApplicationName, $settings->AssertionCredentialsPerms,
//$settings->privateKey, $settings->privateKeyPassword, $settings->assertionType,
//$settings->AssertionCredentialsEmail
//));
$this->ytservice = new Google_YouTubeAnalyticsService($client);
$id = 'contentOwner==aContentOwner';
$start_date = '2013-01-01';
$end_date = '2014-01-01';
$optparams = array('dimensions' => '7DayTotals','sort' => 'day');
$metrics = array('views','estimatedMinutesWatched','averageViewDuration','comments','favoritesAdded','favoritesRemoved','likes','dislikes','shares','subscribersGained','subscribersLost');
$api_response = $metrics;
foreach ($metrics as $metric){
$api = $this->ytservice->reports->query($id, $start_date, $end_date, $metric, $optparams);
if (isset($api['rows'])) $api_response[$metric] = $api['rows'][0][0];
}
return $this->ytservice;
我正在阅读您的问题的第一部分。
刷新令牌是不是访问令牌:它是用于获得访问令牌的令牌。
我认为您没有使用刷新令牌代替访问令牌。还请注意,刷新令牌无需访问受保护的资源:您需要访问令牌。