【问题标题】:What is causing: Error Google_OAuth2.php on line 115原因:第 115 行的错误 Google_OAuth2.php
【发布时间】:2014-02-06 00:14:43
【问题描述】:

编辑 这个特殊的错误是由于我没有正确设置应用程序的错误。在这种情况下,即使代码现在可以工作,我也不知道是什么导致了问题,我需要深入研究内部工作来解决这个问题。 结束

我正在从 Windows7 操作系统命令行运行 PHP 的 Google Drive API QuickStart 示例代码。这是我正在运行的代码:

Google Drive API Quick Start

我可以通过获取授权 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


    【解决方案1】:

    我终于能够从 Google 获取示例代码来运行。我认为问题在于我使用了错误的客户端 ID 和客户端密码。好吧,实际上我一起使用了错误的客户端 ID 凭据应用程序类型。我想我用web application 代替installed application。好吧,我很高兴这是我的错误,而不是 API 的问题。 Native Application 用于 Installed Application。我想这些术语可以互换使用。而Installed Application 用于从Command Line 运行PHP。我注意到的另一件事是,当创建 Native Application 客户端 ID 时,您不能更改重定向 URI。它们会自动设置为:

    urn:ietf:wg:oauth:2.0:oob
    http://localhost
    

    【讨论】:

      猜你喜欢
      • 2012-04-29
      • 1970-01-01
      • 2013-07-12
      • 1970-01-01
      • 1970-01-01
      • 2012-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多