dongleiwei2182 2014-01-16 21:01
浏览 34

原因:第115行错误了Google_OAuth2.php

EDIT This particular error was from my mistake of not setting up the application correctly. It's a situation where, even though the code works now, I have no idea what caused the problem, and I'd need to get deep into the inner workings to figure it out. end

I'm running the Google Drive API QuickStart example code for PHP from Windows7 operating system command line. Here is the code that I am running:

Google Drive API Quick Start

I can get through obtaining an authorization URL, that will successfully return my access token. But after entering the access token, something goes wrong in the `Google_OAuth2.php file supplied by Google to complete the authorization.

Here is a more complete error msg:

Fatal error:

Uncaught exception 'Google_AuthException' with message 'Error fetching OAuth2 access token,

message: 'invalid_request'' in C:\google-api-php-client\src\auth\Google_OAuth2.php:115 Stack trace:

#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} thrown in C:\google-api-php-client\src\auth\Google_OAuth2.php on line 115

This is part of the code from the Google_OAuth2.php file: Line 115 is the last statement 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());
      }
    }

Obviously the error comes before line 115. Line 115 is just printing out that an error occurred somewhere before. I don't know if it's the code, or something I'm doing wrong. I know that every time a new URL authorization request is put in, I get a different access code back. My client secret and client ID shouldn't ever need to change. I have no idea how to make this work at this point.

  • 写回答

1条回答 默认 最新

  • dongpeng0127 2014-01-17 21:10
    关注

    I was finally able to get the example code from Google to run. I think the problem was that I was using the wrong Client ID and Client Secret. Well, actually I was using the wrong Client ID Credential application type all together. I think I was using the web application for an installed application. Well, I'm glad it was my error, and not a problem with the API. The Native Application is for the Installed Application. I guess those terms get used interchangeably. And the Installed Application is for running PHP from the Command Line. The other thing I noticed, is that when the Native Application client ID is created, you can NOT change the Redirect URIs. They are automatically set to:

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

报告相同问题?

悬赏问题

  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入