16

我遵循了所有这些步骤。

https://developers.google.com/+/web/signin/

我有客户 ID 和客户密码。

我现在获得了访问令牌,如何获取带有访问令牌的用户资料和电子邮件?以及如何检查用户是否登录?

4

4 回答 4

26

使用 OAuth2,您可以通过范围参数请求权限。(文档。)我想你想要的范围是https://www.googleapis.com/auth/userinfo.emailhttps://www.googleapis.com/auth/userinfo.profile

然后,一旦您获得访问令牌,就可以轻松获取个人资料信息。(我假设您已经能够将返回的授权码兑换为访问令牌?)只需向https://www.googleapis.com/oauth2/v1/userinfo?access_token= {accessToken} 发出 get 请求,它会返回配置文件数据的 JSON 数组,包括电子邮件:

{
    "id": "00000000000000",
    "email": "fred.example@gmail.com",
    "verified_email": true,
    "name": "Fred Example",
    "given_name": "Fred",
    "family_name": "Example",
    "picture": "https://lh5.googleusercontent.com/-2Sv-4bBMLLA/AAAAAAAAAAI/AAAAAAAAABo/bEG4kI2mG0I/photo.jpg",
    "gender": "male",
    "locale": "en-US"
}

没有保证,但试试这个:

$url = "https://www.googleapis.com/oauth2/v1/userinfo";
$request = apiClient::$io->makeRequest($client->sign(new apiHttpRequest($url, 'GET')));

if ((int)$request->getResponseHttpCode() == 200) {
    $response = $request->getResponseBody();
    $decodedResponse = json_decode($response, true);
    //process user info
  } else {
    $response = $request->getResponseBody();
    $decodedResponse = json_decode($response, true);
    if ($decodedResponse != $response && $decodedResponse != null && $decodedResponse['error']) {
      $response = $decodedResponse['error'];
    }
  }
}
于 2013-02-27T05:22:08.080 回答
8

尝试这个

$accessToken = 'access token';
$userDetails = file_get_contents('https://www.googleapis.com/oauth2/v1/userinfo?access_token=' . $accessToken);
$userData = json_decode($userDetails);

if (!empty($userData)) {

  $googleUserId = '';
  $googleEmail = '';
  $googleVerified = '';
  $googleName = '';
  $googleUserName = '';

  if (isset($userData->id)) {
    $googleUserId = $userData->id;
  }
  if (isset($userData->email)) {
    $googleEmail = $userData->email;
    $googleEmailParts = explode("@", $googleEmail);
    $googleUserName = $googleEmailParts[0];
  }
  if (isset($userData->verified_email)) {
    $googleVerified = $userData->verified_email;
  }
  if (isset($userData->name)) {
    $googleName = $userData->name;
  }
} else {

  echo "Not logged In";
}
于 2013-02-27T05:18:44.107 回答
1

您只需将此行添加到您的scope Open your 中Application.cfc,然后添加此代码

     <cfset request.oauthSettings = 
           {scope = "https://www.googleapis.com/auth/userinfo.email+https://www.googleapis.com/auth/userinfo.profile",
                                    client_id = "Your-id",
                                    client_secret = "your-secret",
                                    redirect_uri = "redirect-page",
                                    state = "optional"} />

现在您可以从可以这样调用的函数中获取电子邮件

    <cfscript>              
        public function getProfile(accesstoken) {

            var h = new com.adobe.coldfusion.http();
            h.setURL("https://www.googleapis.com/oauth2/v1/userinfo");
            h.setMethod("get");
            h.addParam(type="header",name="Authorization",value="OAuth #accesstoken#");
            h.addParam(type="header",name="GData-Version",value="3");
            h.setResolveURL(true);
            var result = h.send().getPrefix();
            return deserializeJSON(result.filecontent.toString());
        }       
    </cfscript>

            <cfoutput>
            <cfset show = getProfile(session.ga_accessToken)>
            <cfdump var="#show#">
           </cfoutput>

希望这可以帮助很多人解决这个问题。:)

于 2013-06-28T18:57:40.953 回答
0
$access_token = 'your access token';
$headers = array('Content-Type: Application/json');
$endpoint = "https://www.googleapis.com/oauth2/v1/userinfo?access_token=".$access_token;


$soap_do = curl_init();
curl_setopt($soap_do, CURLOPT_URL, $endPoint);
curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true);
curl_setopt($soap_do, CURLOPT_HTTPHEADER, $header);

curl_setopt($soap_do, CURLOPT_FAILONERROR, true);
$result = curl_exec($soap_do);
于 2020-12-30T07:22:04.630 回答