• <bdo id='eHu79'></bdo><ul id='eHu79'></ul>

  • <tfoot id='eHu79'></tfoot>
  • <legend id='eHu79'><style id='eHu79'><dir id='eHu79'><q id='eHu79'></q></dir></style></legend>
    <i id='eHu79'><tr id='eHu79'><dt id='eHu79'><q id='eHu79'><span id='eHu79'><b id='eHu79'><form id='eHu79'><ins id='eHu79'></ins><ul id='eHu79'></ul><sub id='eHu79'></sub></form><legend id='eHu79'></legend><bdo id='eHu79'><pre id='eHu79'><center id='eHu79'></center></pre></bdo></b><th id='eHu79'></th></span></q></dt></tr></i><div id='eHu79'><tfoot id='eHu79'></tfoot><dl id='eHu79'><fieldset id='eHu79'></fieldset></dl></div>

      1. <small id='eHu79'></small><noframes id='eHu79'>

        从 Google OAuth 2.0 PHP API 获取用户信息

        Get Userinfo from Google OAuth 2.0 PHP API(从 Google OAuth 2.0 PHP API 获取用户信息)

        • <tfoot id='0BKmN'></tfoot>

            <legend id='0BKmN'><style id='0BKmN'><dir id='0BKmN'><q id='0BKmN'></q></dir></style></legend>
                <tbody id='0BKmN'></tbody>

              • <bdo id='0BKmN'></bdo><ul id='0BKmN'></ul>
                <i id='0BKmN'><tr id='0BKmN'><dt id='0BKmN'><q id='0BKmN'><span id='0BKmN'><b id='0BKmN'><form id='0BKmN'><ins id='0BKmN'></ins><ul id='0BKmN'></ul><sub id='0BKmN'></sub></form><legend id='0BKmN'></legend><bdo id='0BKmN'><pre id='0BKmN'><center id='0BKmN'></center></pre></bdo></b><th id='0BKmN'></th></span></q></dt></tr></i><div id='0BKmN'><tfoot id='0BKmN'></tfoot><dl id='0BKmN'><fieldset id='0BKmN'></fieldset></dl></div>

                <small id='0BKmN'></small><noframes id='0BKmN'>

                  本文介绍了从 Google OAuth 2.0 PHP API 获取用户信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试使用 Google Oauth API 来获取用户信息.它非常适合 Google Plus API,但我正在尝试创建备份,以防用户没有 google plus 帐户.身份验证过程是正确的,我什至获得了 $userinfo 对象,但我究竟如何访问这些属性.我试过 $userinfo->get() 但它只返回用户的 id.

                  我做错了吗?这是我正在使用的代码...

                  require_once '../../src/Google_Client.php';require_once '../../src/contrib/Google_Oauth2Service.php';session_start();$client = new Google_Client();$client->setApplicationName("Google+ PHP Starter Application");//访问 https://code.google.com/apis/console 生成您的//oauth2_client_id、oauth2_client_secret,并注册您的 oauth2_redirect_uri.$client->setClientId('*********************');$client->setClientSecret('****************');$client->setRedirectUri('***************');$client->setDeveloperKey('**************');$plus = new Google_Oauth2Service($client);如果(isset($_REQUEST ['注销'])){未设置($_SESSION['access_token']);}如果(isset($_GET['代码'])){$client->authenticate($_GET['code']);$_SESSION['access_token'] = $client->getAccessToken();header('位置:http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);}如果(isset($_SESSION['access_token'])){$client->setAccessToken($_SESSION['access_token']);}if ($client->getAccessToken()){$userinfo = $plus->userinfo;print_r($userinfo->get());} 别的{$authUrl = $client->createAuthUrl();}?><!doctype html><头><meta charset="utf-8"><link rel='stylesheet' href='style.css'/><身体><header><h1>Google+ 示例应用</h1></header><div class="box"><?php if(isset($personMarkup)): ?><div class="me"><?php print $personMarkup ?></div><?php endif ?><?php如果(isset($authUrl)){print "<a class='login' href='$authUrl'>连接我!</a>";} 别的 {打印 "<a class='logout' href='?logout'>Logout</a>";}?>

                  谢谢...

                  **编辑***缺少范围--已添加

                   $client->setScopes(array('https://www.googleapis.com/auth/userinfo.email','https://www.googleapis.com/auth/userinfo.profile'));

                  现在工作...

                  解决方案

                  缺少作用域

                  $client->setScopes(array('https://www.googleapis.com/auth/userinfo.email','https://www.googleapis.com/auth/userinfo.profile'));

                  现在就像一个魅力!

                  I am trying to use Google Oauth API to get userinfo. It works perfectly for Google Plus API but I am trying to create a backup in case the user doesn't have google plus account. The authentication process is correct and I even get the $userinfo object but how exactly do I access the properties. I tried $userinfo->get() but it only return the id of the user.

                  Am I doing something wrong? Here is the code that I am using...

                  require_once '../../src/Google_Client.php';
                  require_once '../../src/contrib/Google_Oauth2Service.php';
                  
                  session_start();
                  
                  $client = new Google_Client();
                  $client->setApplicationName("Google+ PHP Starter Application");
                  // Visit https://code.google.com/apis/console to generate your
                  // oauth2_client_id, oauth2_client_secret, and to register your oauth2_redirect_uri.
                   $client->setClientId('*********************');
                   $client->setClientSecret('**************');
                   $client->setRedirectUri('***************');
                   $client->setDeveloperKey('**************');
                  $plus = new Google_Oauth2Service($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()) 
                  {
                      $userinfo = $plus->userinfo;
                      print_r($userinfo->get());
                  
                  } else 
                  {
                      $authUrl = $client->createAuthUrl();
                  }
                  ?>
                  <!doctype html>
                  <html>
                  <head>
                    <meta charset="utf-8">
                    <link rel='stylesheet' href='style.css' />
                  </head>
                  <body>
                  <header><h1>Google+ Sample App</h1></header>
                  <div class="box">
                  
                  <?php if(isset($personMarkup)): ?>
                  <div class="me"><?php print $personMarkup ?></div>
                  <?php endif ?>
                  
                  <?php
                    if(isset($authUrl)) {
                      print "<a class='login' href='$authUrl'>Connect Me!</a>";
                    } else {
                     print "<a class='logout' href='?logout'>Logout</a>";
                    }
                  ?>
                  </div>
                  </body>
                  </html>
                  

                  Thanks...

                  **EDIT*** Was missing Scopes--Added

                   $client->setScopes(array('https://www.googleapis.com/auth/userinfo.email','https://www.googleapis.com/auth/userinfo.profile'));
                  

                  works now...

                  解决方案

                  Was missing scopes

                  $client->setScopes(array('https://www.googleapis.com/auth/userinfo.email','https://www.googleapis.com/auth/userinfo.profile'));
                  

                  Works like a charm now!

                  这篇关于从 Google OAuth 2.0 PHP API 获取用户信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                  <legend id='WBF8O'><style id='WBF8O'><dir id='WBF8O'><q id='WBF8O'></q></dir></style></legend>

                      <tbody id='WBF8O'></tbody>
                    <tfoot id='WBF8O'></tfoot>

                    <small id='WBF8O'></small><noframes id='WBF8O'>

                    • <bdo id='WBF8O'></bdo><ul id='WBF8O'></ul>

                      <i id='WBF8O'><tr id='WBF8O'><dt id='WBF8O'><q id='WBF8O'><span id='WBF8O'><b id='WBF8O'><form id='WBF8O'><ins id='WBF8O'></ins><ul id='WBF8O'></ul><sub id='WBF8O'></sub></form><legend id='WBF8O'></legend><bdo id='WBF8O'><pre id='WBF8O'><center id='WBF8O'></center></pre></bdo></b><th id='WBF8O'></th></span></q></dt></tr></i><div id='WBF8O'><tfoot id='WBF8O'></tfoot><dl id='WBF8O'><fieldset id='WBF8O'></fieldset></dl></div>