【问题标题】:Google OAuth 2 PHP call to userinfo谷歌 OAuth 2 PHP 调用用户信息
【发布时间】:2012-01-09 16:48:06
【问题描述】:

我正在尝试使用 Google 的 OAuth2 API。在他们的通用文档中,他们提到了一个名为 UserInfo: http://code.google.com/apis/accounts/docs/OAuth2Login.html#userinfocall 的调用,它可以让我检索用户 ID、电子邮件、姓名和其他基本信息。

但是我在他们的 PHP 客户端库中找不到它:https://code.google.com/p/google-api-php-client/

它在哪里?

【问题讨论】:

    标签: php google-api oauth-2.0


    【解决方案1】:

    我已经发布了一个适合我的解决方案,一个用于检索 UserInfo 的 Google PHP 客户端库的补丁,在这个类似的问题中:How to identify a Google OAuth2 user?

    【讨论】:

      【解决方案2】:
      【解决方案3】:

      Google OAuth2 API 已更改 - 现在您获取用户信息的方式如下:

      <?php
      
      require_once('google-api-php-client-1.1.7/src/Google/autoload.php');
      
      const TITLE = 'My amazing app';
      const REDIRECT = 'https://example.com/myapp/';
      
      session_start();
      
      $client = new Google_Client();
      $client->setApplicationName(TITLE);
      $client->setClientId('REPLACE_ME.apps.googleusercontent.com');
      $client->setClientSecret('REPLACE_ME');
      $client->setRedirectUri(REDIRECT);
      $client->setScopes(array(Google_Service_Plus::PLUS_ME));
      $plus = new Google_Service_Plus($client);
      
      if (isset($_REQUEST['logout'])) {
              unset($_SESSION['access_token']);
      }
      
      if (isset($_GET['code'])) {
              if (strval($_SESSION['state']) !== strval($_GET['state'])) {
                      error_log('The session state did not match.');
                      exit(1);
              }
      
              $client->authenticate($_GET['code']);
              $_SESSION['access_token'] = $client->getAccessToken();
              header('Location: ' . REDIRECT);
      }
      
      if (isset($_SESSION['access_token'])) {
              $client->setAccessToken($_SESSION['access_token']);
      }
      
      if ($client->getAccessToken() && !$client->isAccessTokenExpired()) {
              try {
                      $me = $plus->people->get('me');
                      $body = '<PRE>' . print_r($me, TRUE) . '</PRE>';
              } catch (Google_Exception $e) {
                      error_log($e);
                      $body = htmlspecialchars($e->getMessage());
              }
              # the access token may have been updated lazily
              $_SESSION['access_token'] = $client->getAccessToken();
      } else {
              $state = mt_rand();
              $client->setState($state);
              $_SESSION['state'] = $state;
              $body = sprintf('<P><A HREF="%s">Login</A></P>',
                  $client->createAuthUrl());
      }
      
      ?>
      
      <!DOCTYPE HTML>
      <HTML>
      <HEAD>
              <TITLE><?= TITLE ?></TITLE>
      </HEAD>
      <BODY>
              <?= $body ?>
              <P><A HREF="<?= REDIRECT ?>?logout">Logout</A></P>
      </BODY>
      </HTML>
      

      别忘了-

      1. Google API console 获取 Web 客户端 ID 和密码
      2. 在同一地点授权https://example.com/myapp/

      您可以在Youtube GitHub找到官方示例。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-08-20
        • 1970-01-01
        • 1970-01-01
        • 2019-12-06
        • 1970-01-01
        • 2013-01-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多