2011-08-20 275 views

回答

84

一下添加到范围 - https://www.googleapis.com/auth/userinfo.profile

和授权完成后,得到的信息 - https://www.googleapis.com/oauth2/v1/userinfo?alt=json

它的东西负载 - 包括姓名,公开档案URL地址,性别,照片等

+0

我用上面的网址,但无法获取用户的个人资料。只有'{'。 Plz可以发布一些代码或链接。提前致谢。 – Panache

+0

可以详细说明如何使用上述网址获取用户个人资料... plz .. – Panache

+0

严正,只需通过OAuth 2授权该请求即可。该URL返回当前登录用户的数据。如果您发出请求但不发送OAuth标头,它会给您一个错误。 –

75

范围 - https://www.googleapis.com/auth/userinfo.profile

return youraccess_token = access_token 

得到https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token=youraccess_token

你会得到JSON:

{ 
"id": "xx", 
"name": "xx", 
"given_name": "xx", 
"family_name": "xx", 
"link": "xx", 
"picture": "xx", 
"gender": "xx", 
"locale": "xx" 
} 

要塔希尔·亚辛:

这是一个PHP的例子。
您可以使用json_decode函数来获取userInfo数组。

$q = 'https://www.googleapis.com/oauth2/v1/userinfo?access_token=xxx'; 
$json = file_get_contents($q); 
$userInfoArray = json_decode($json,true); 
$googleEmail = $userInfoArray['email']; 
$googleFirstName = $userInfoArray['given_name']; 
$googleLastName = $userInfoArray['family_name']; 
+0

如何获取有关用户的更多信息? –

+1

如何使用他们的回应? –

+3

它只给出编号 –

25

此范围https://www.googleapis.com/auth/userinfo.profile现在已被弃用。请看https://developers.google.com/+/api/auth-migration#timetable

您将使用获得资料信息的新范围是:配置文件或https://www.googleapis.com/auth/plus.login

和端点 - https://www.googleapis.com/plus/v1/people/ {} userId的 - 用户id可以只是“我”为当前登录的用户。

+0

这是一个对整合未来证明信息的重要平安。更多关于已弃用范围的信息https://developers.google.com/+/web/api/rest/oauth –

4

我正在使用Google API for .Net,但毫无疑问,您可以使用其他版本的API以相同方式获取此信息。 由于user872858提到,范围userinfo.profile已被弃用(google article)。

要获取用户的个人资料信息,我使用下面的代码(重新编写部分来自google's example):

IAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow(
            new GoogleAuthorizationCodeFlow.Initializer 
             { 
              ClientSecrets = Secrets, 
              Scopes = new[] { PlusService.Scope.PlusLogin,"https://www.googleapis.com/auth/plus.profile.emails.read" } 
             });  
TokenResponse _token = flow.ExchangeCodeForTokenAsync("", code, "postmessage", 
           CancellationToken.None).Result; 

        // Create an authorization state from the returned token. 
        context.Session["authState"] = _token; 

        // Get tokeninfo for the access token if you want to verify. 
        Oauth2Service service = new Oauth2Service(
        new Google.Apis.Services.BaseClientService.Initializer()); 
        Oauth2Service.TokeninfoRequest request = service.Tokeninfo(); 
        request.AccessToken = _token.AccessToken; 
        Tokeninfo info = request.Execute(); 
        if (info.VerifiedEmail.HasValue && info.VerifiedEmail.Value) 
        { 
         flow = new GoogleAuthorizationCodeFlow(
            new GoogleAuthorizationCodeFlow.Initializer 
             { 
              ClientSecrets = Secrets, 
              Scopes = new[] { PlusService.Scope.PlusLogin } 
              }); 

         UserCredential credential = new UserCredential(flow, 
                   "me", _token); 
         _token = credential.Token; 
         _ps = new PlusService(
           new Google.Apis.Services.BaseClientService.Initializer() 
           { 
            ApplicationName = "Your app name", 
            HttpClientInitializer = credential 
           }); 
         Person userProfile = _ps.People.Get("me").Execute(); 
        } 

比,你几乎可以访问使用USERPROFILE东西。

更新:要使此代码正常工作,您必须在Google登录按钮上使用适当的作用域。例如我的按钮:

 <button class="g-signin" 
      data-scope="https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/plus.profile.emails.read" 
      data-clientid="646361778467-nb2uipj05c4adlk0vo66k96bv8inqles.apps.googleusercontent.com" 
      data-accesstype="offline" 
      data-redirecturi="postmessage" 
      data-theme="dark" 
      data-callback="onSignInCallback" 
      data-cookiepolicy="single_host_origin" 
      data-width="iconOnly"> 
    </button> 
16

我正在使用PHP并通过使用1.1版来解决此问题。的google-api-php-client

假设下面的代码4用于一个用户重定向到谷歌认证页面:

$client = new Google_Client(); 
$client->setAuthConfigFile('/path/to/config/file/here'); 
$client->setRedirectUri('https://redirect/url/here'); 
$client->setAccessType('offline'); //optional 
$client->setScopes(['profile']); //or email 
$auth_url = $client->createAuthUrl(); 
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL)); 
exit(); 

假设有效的认证码返回到redirect_url,下面将产生从所述认证令牌代码以及提供基本的配置文件信息:

//assuming a successful authentication code is return 
$authentication_code = 'code-returned-by-google'; 
$client = new Google_Client(); 
//.... configure $client object code goes here 
$client->authenticate($authentication_code); 
$token_data = $client->getAccessToken(); 

//get user email address 
$google_oauth =new Google_Service_Oauth2($client); 
$google_account_email = $google_oauth->userinfo->get()->email; 
//$google_oauth->userinfo->get()->familyName; 
//$google_oauth->userinfo->get()->givenName; 
//$google_oauth->userinfo->get()->name; 
//$google_oauth->userinfo->get()->gender; 
//$google_oauth->userinfo->get()->picture; //profile picture 

但是,不返回位置。 New YouTube accounts don't have YouTube specific usernames

+0

如何获取位置? – SoftSan

+0

使用此范围我无法获取性别信息(我已将性别信息公开)。我已经尝试过oauth playground developers.google.com/oauthplayground。我想在服务器端使用REST API来做到这一点。你能帮助我吗? –

+0

既不能获得性别。而且在某些情况下,除了电子邮件外,没有任何内容可以返回想法? –

1

如果您处于客户端Web环境中,则新的auth2 javascript API包含非常需要的getBasicProfile()函数,该函数返回用户的姓名,电子邮件和图像URL。

​​

+0

但是,实际的API网址是什么?我查看了文档,找不到实际的API URL。谷歌似乎推动我们到他们的SDK,但不是每个人都想使用SDK。 – Supertecnoboff