google plus oauth用php登录信息不足

时间:2013-12-18 20:13:53

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

我在这里:

https://developers.google.com/+/web/signin/server-side-flow

在步骤7和8上有引用变量$ request但是这个变量没有被初始化,因此从它们提供的示例中复制和粘贴不起作用,我从步骤7的第一行得到500服务器错误或者单独的步骤8,使用$ request的第8行,从未从他们的示例中初始化。

$code = $request->getContent();

1 个答案:

答案 0 :(得分:2)

您正在查看的示例代码使用包含$request$response值的Twig来简化RESTful端点。

以下代码执行没有Twig依赖项的等效代码:

<?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_PlusService.php';

  $client = new Google_Client();

  // CLIENT ID / Secret from https://code.google.com/apis/console
  $CLIENT_ID = 'YOUR_CLIENT_ID';
  $client->setClientId($CLIENT_ID);
  $client->setClientSecret('YOUR_CLIENT_SECRET');

  // CUSTOM redirect URI assuming code from JavaScript callback
  $client->setRedirectUri('postmessage');

  $plus = new Google_PlusService($client);

  // Code from the client (returned in signinCallback, or in token on Android)
  $code = file_get_contents('php://input');

  // Exchange the OAuth 2.0 authorization code for user credentials.
  $client->authenticate($code);
  $token = json_decode($client->getAccessToken());

  // Verify the token
  $reqUrl = 'https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=' .
          $token->access_token;

  $req = new Google_HttpRequest($reqUrl);

  $tokenInfo = json_decode(
      $client::getIo()->authenticatedRequest($req)->getResponseBody());

  // If there was an error in the token info, abort.
  if ($tokenInfo->error) {
    print $tokenInfo->error;
  }
  // Make sure the token we got is for our app.
  if ($tokenInfo->audience != CLIENT_ID) {
    print "Token's client ID does not match app's.";
  }

  print 'Token from result: ' . print_r($token, true);