Google Plus登录API Invalid_request PHP客户端

时间:2013-10-22 03:46:51

标签: php google-plus google-api-php-client

很多人有类似的错误......没有一个回答我的问题。试图让用户登录他们的Google Plus并存储刷新令牌,以便我可以代表他们发布他们的时刻。甚至无法通过认证令牌。这是我的代码:

require $_SERVER['DOCUMENT_ROOT'] . '/members/assets/lib/google/Google_Client.php';
require $_SERVER['DOCUMENT_ROOT'] . '/members/assets/lib/google/contrib/Google_PlusService.php';

$client = new Google_Client();
    $client->setApplicationName("Americal Lose Weight");
    $client->setClientId($oauth2_client_id);
    $client->setClientSecret($oauth_client_secret);
    $client->setRedirectUri($oauth2_redirect_uri);

    $plus = new Google_PlusService($client);

if (isset($_REQUEST['logout'])) {
  unset($_SESSION['access_token']);
}

if (isset($_GET['code'])) {
  $client->authenticate($_GET['code']);
  $_SESSION['access_token'] = $client->getAccessToken();
  header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}

if (isset($_SESSION['access_token'])) {
  $client->setAccessToken($_SESSION['access_token']);
}

if ($client->getAccessToken()) {
  $me = $plus->people->get('me');

  $url = filter_var($me['url'], FILTER_VALIDATE_URL);
  $img = filter_var($me['image']['url'], FILTER_VALIDATE_URL);
  $name = filter_var($me['displayName'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
  $personMarkup = "<a rel='me' href='$url'>$name</a><div><img src='$img'></div>";

  $optParams = array('maxResults' => 100);
  $activities = $plus->activities->listActivities('me', 'public', $optParams);
  $activityMarkup = '';
  foreach($activities['items'] as $activity) {
    $url = filter_var($activity['url'], FILTER_VALIDATE_URL);
    $title = filter_var($activity['title'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
    $content = filter_var($activity['object']['content'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
    $activityMarkup .= "<div class='activity'><a href='$url'>$title</a><div>$content</div></div>";
  }

  // The access token may have been updated lazily.
  $_SESSION['access_token'] = $client->getAccessToken();
} else {
  $authUrl = $client->createAuthUrl();
}

if(isset($authUrl)) {
    header("Location: $authUrl");
} else {
    var_dump($_SESSION);    
}

当然我将我的客户端ID和客户端密码存储在配置文件中,该文件在此内容之前调用。

auth url加载,我可以选择我的Google Plus帐号...但在回调它给我

  

未捕获的异常“Google_AuthException”,并显示错误“获取OAuth2访问令牌时出错,消息:'invalid_request'

你可以在http://members.americaloseweight.com/auth/login.php?app=google试试 我的重定向网址指向此页面。

那么我做错了什么?

修改

在第97行的Google_OAuth2.php中,它调用了makeRequest函数。

$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
  )));

当我转发作为请求返回的内容时,我得到:

Google_HttpRequest Object
(
    [batchHeaders:Google_HttpRequest:private] => Array
        (
            [Content-Type] => application/http
            [Content-Transfer-Encoding] => binary
            [MIME-Version] => 1.0
            [Content-Length] => 
        )

    [url:protected] => https://accounts.google.com/o/oauth2/token
    [requestMethod:protected] => POST
    [requestHeaders:protected] => Array
        (
            [content-type] => application/x-www-form-urlencoded
            [content-length] => 272
        )

    [postBody:protected] => code=4%2FPWcMMWU80cd20kvppA0mlA8ldCYl.grjspKy0hxAUshQV0ieZDAq-J97AgwI&grant_type=authorization_code&redirect_uri=http%3A%2F%2Fmembers.americaloseweight.com%2Fauth%2Flogin.php%3Fapp%3Dgoogle&client_id=344482743517-2pn1hngv5sv3mf4bk70pjvq2ufkf2uii.apps.googleusercontent.com
    [userAgent:protected] => Americal Lose Weight google-api-php-client/0.6.5
    [responseHttpCode:protected] => 400
    [responseHeaders:protected] => Array
        (
            [cache-control] => no-cache, no-store, max-age=0, must-revalidate
            [pragma] => no-cache
            [expires] => Fri, 01 Jan 1990 00:00:00 GMT
            [date] => Tue, 22 Oct 2013 13:49:18 GMT
            [content-type] => application/json
            [x-content-type-options] => nosniff
            [x-frame-options] => SAMEORIGIN
            [x-xss-protection] => 1; mode=block
            [server] => GSE
            [alternate-protocol] => 443:quic
            [transfer-encoding] => chunked
        )

    [responseBody:protected] => {
  "error" : "invalid_request"
}
    [accessKey] => 
)

1 个答案:

答案 0 :(得分:2)

检查您的客户端密码是否正确 - 确保字符串末尾没有额外的空格或类似内容。还要确保您的客户端ID是正确的类型 - 它是Web应用程序的客户端ID。