【问题标题】:Why are my Google PHP Client headers always empty trying to oauth2为什么我的 Google PHP 客户端标头在尝试 oauth2 时总是为空
【发布时间】:2014-05-30 21:55:10
【问题描述】:

问题

我得到的最令人担忧的错误是invalid_clientinvalid_grant。我正在使用来自 oauthplayground 的访问令牌。在选择和授权 API 范围时,我还填充了配置选项以使用我自己的客户端 ID 和机密,其类型必须为 Web Application,因为 Installed/Native ApplicationService Account 会引发 mismatch_redirect_uri 错误和您不能在控制台中将重定向 uri 添加到这些类型。

我喜欢我读过的许多其他类似问题的帖子,需要使用 PHP 客户端运行 CRON 作业以 CURL Google 的 API。如果您能准确地解释如何在 Web / LAMP 环境中为此目的使用 Installed 或 Service Accounts,我会全力以赴

EMPTY 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"
]
}

OAUTH2 的 PHP 代码并进行简单报告

    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;

【问题讨论】:

    标签: php curl oauth-2.0 google-api google-api-php-client


    【解决方案1】:

    我正在阅读你问题的第一部分。

    刷新令牌不是访问令牌:它是用于获取访问令牌的令牌。

    我假设您没有使用刷新令牌代替访问令牌。另请注意,访问受保护资源不需要刷新令牌:您需要一个访问令牌。

    【讨论】:

    • 谢谢 srikanth,但我意识到了这一点,并且没有发送刷新令牌。为了清楚起见,我将编辑问题
    猜你喜欢
    • 1970-01-01
    • 2017-06-15
    • 1970-01-01
    • 1970-01-01
    • 2021-10-30
    • 2015-09-23
    • 2019-01-28
    • 1970-01-01
    • 2011-11-08
    相关资源
    最近更新 更多