无法在PHP中使用Google OAuth2交换令牌的授权码

时间:2014-10-05 19:07:08

标签: php oauth google-drive-api google-oauth

我的代码在这里: google.php位于http://127.0.0.1/projects/google/google.php

<?php 
require 'vendor/autoload.php';
session_start();

$client_id = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.apps.googleusercontent.com';
$client_secret = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$redirect_uri = 'http://127.0.0.1/projects/google/google.php';
$scope = 'https://www.googleapis.com/auth/drive.readonly';
$api_key ='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$code = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';

$client = new Google_Client();
$client->setApplicationName("Google Drive PHP SDK APP NAME");
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->addScope($scope);
$client->setAccessType("offline");
$client->setDeveloperKey($api_key);

$authUrl = $client->createAuthUrl();
echo $authUrl;

if (isset($_GET['code'])) {
    $client->authenticate($_GET['code']);
    // >>>ERROR HERE<<<
}


$driveSdk = new Google_Service_Drive($client);
$result = $driveSdk->files->listFiles();

访问$ authUrl(https://accounts.google.com/o/oauth2/auth?response_type=code&redirect_uri=http%3A%2F%2F127.0.0.1%2Fprojects%2Fgoogle%2Fgoogle.php&client_id=XXXXXXXXXXXXXXXXXXXXXXXXXXX.apps.googleusercontent.com&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.readonly&access_type=offline&approval_prompt=auto

之后

我从谷歌获取代码,如http://127.0.0.1/projects/google/google.php?code=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

我在$ client-&gt; authenticate中使用它($ _ GET [&#39; code&#39;]);

但是我没有为令牌交换auth代码,而是Fatal error: Uncaught exception 'Google_Auth_Exception' with message 'Error fetching OAuth2 access token, message: 'invalid_client'' in D:\EasyPHP-DevServer-14.1VC11\data\localweb\projects\google\vendor\google\apiclient\src\Google\Auth\OAuth2.php:132 Stack trace: #0 D:\EasyPHP-DevServer-14.1VC11\data\localweb\projects\google\vendor\google\apiclient\src\Google\Client.php(130): Google_Auth_OAuth2->authenticate('4/v-8aFUcb0YyPS...') #1 D:\EasyPHP-DevServer-14.1VC11\data\localweb\projects\google\google.php(31): Google_Client->authenticate('4/v-8aFUcb0YyPS...') #2 {main} thrown in D:\EasyPHP-DevServer-14.1VC11\data\localweb\projects\google\vendor\google\apiclient\src\Google\Auth\OAuth2.php on line 132

我记录了CURL: 请求HTTP标头是:

POST /o/oauth2/token HTTP/1.1 Host: accounts.google.com Accept: */* content-type: application/x-www-form-urlencoded content-length: 291 

请求帖子正文:

code=4%2FSevLUuq8mtyfUYYb2FSZ5Q5mYexF.smYRm3eONugRPvB8fYmgkJzTRFXKkQI&grant_type=authorization_code&redirect_uri=http%3A%2F%2F127.0.0.1%2Fprojects%2Fgoogle%2Fgoogle.php&client_id=360693274514-a9orr65tr5f9elsfkfng2htls6cr308b.apps.googleusercontent.com&client_secret=Gb02td3zZv1T01IQt-sGlFVwH

回应是:

HTTP/1.1 401 Unauthorized Content-Type: application/json Cache-Control: no-cache, no-store, max-age=0, must-revalidate Pragma: no-cache Expires: Fri, 01 Jan 1990 00:00:00 GMT Date: Mon, 06 Oct 2014 11:36:42 GMT X-Content-Type-Options: nosniff X-Frame-Options: SAMEORIGIN X-XSS-Protection: 1; mode=block Server: GSE Alternate-Protocol: 443:quic,p=0.01 Transfer-Encoding: chunked { "error" : "invalid_client" }

好像一切都做对了,但仍然是错误。

我无法得到它。任何帮助将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:0)

Google SDK的第115行没有错,请仔细检查您的$ client_id。似乎响应错误是正确的。

相关问题