1

很多人有类似的错误......没有一个回答我的问题。试图让用户登录到他们的 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 和客户端机密存储在一个配置文件中,该文件在此之前被调用。

身份验证网址加载,我可以选择我的 Google Plus 帐户...但在回调时它给了我

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

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

那么我做错了什么?

编辑

在第 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] => 
)
4

1 回答 1

2

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

于 2013-10-22T08:37:13.510 回答