【发布时间】:2014-02-06 00:14:43
【问题描述】:
编辑 这个特殊的错误是由于我没有正确设置应用程序的错误。在这种情况下,即使代码现在可以工作,我也不知道是什么导致了问题,我需要深入研究内部工作来解决这个问题。 结束
我正在从 Windows7 操作系统命令行运行 PHP 的 Google Drive API QuickStart 示例代码。这是我正在运行的代码:
我可以通过获取授权 URL,这将成功返回我的访问令牌。但是在输入访问令牌后,谷歌提供的用于完成授权的`Google_OAuth2.php文件出现问题。
这是一个更完整的错误消息:
致命错误:
未捕获的异常“Google_AuthException”和消息“获取错误” OAuth2 访问令牌,
消息:'invalid_request'' 在 C:\google-api-php-client\src\auth\Google_OAuth2.php:115 堆栈跟踪:
#0 C:\google-api-php-client\src\Google_Client.php(127): Google_OAuth2->authenticate(Array, '4/HW2t_MZIxatq6...')
#1 C:\Users\Gigabyte\MyWebsite\Take_Validation.php(10): Google_Client->authenticate('4/HW2t_MZIxatq6...')
#2 {main} 投入 C:\google-api-php-client\src\auth\Google_OAuth2.php 在第 115 行
这是来自 Google_OAuth2.php 文件的部分代码:第 115 行是最后一条语句 throw new Google_AuthException
/**
* @param $service
* @param string|null $code
* @throws Google_AuthException
* @return string
*/
public function authenticate($service, $code = null) {
if (!$code && isset($_GET['code'])) {
$code = $_GET['code'];
}
if ($code) {
// We got here from the redirect from a successful authorization grant, fetch the access token
$request = Google_Client::$io->makeRequest(new Google_HttpRequest(self::OAUTH2_TOKEN_URI, 'POST', array(), array(
'code' => $code,
'grant_type' => 'authorization_code',
'redirect_uri' => $this->redirectUri,
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret
)));
if ($request->getResponseHttpCode() == 200) {
$this->setAccessToken($request->getResponseBody());
$this->token['created'] = time();
return $this->getAccessToken();
} else {
$response = $request->getResponseBody();
$decodedResponse = json_decode($response, true);
if ($decodedResponse != null && $decodedResponse['error']) {
$response = $decodedResponse['error'];
}
throw new Google_AuthException("Error fetching OAuth2 access token, message: '$response'", $request->getResponseHttpCode());
}
}
显然错误出现在第 115 行之前。第 115 行只是打印出之前某处发生过错误。我不知道这是代码,还是我做错了什么。我知道每次输入一个新的 URL 授权请求时,我都会得到一个不同的访问代码。我的客户密码和客户 ID 永远不需要更改。目前我不知道如何进行这项工作。
【问题讨论】:
标签: php google-drive-api google-drive-realtime-api