113

Google API 経由でユーザーのプロフィールから情報を取得することはできますか? 可能であれば、どの API を使用すればよいですか?

私はそのような情報に興味があります:

また、ユーザーのプロファイルから他の情報を取得することもクールです。

4

9 に答える 9

135

これをスコープに追加します - https://www.googleapis.com/auth/userinfo.profile

認証が完了したら、https://www.googleapis.com/oauth2/v1/userinfo?alt=jsonから情報を取得します

名前、公開プロフィールの URL、性別、写真など、たくさんのものがあります。

于 2011-08-21T13:10:38.543 に答える
96

スコープ - 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'];
于 2011-11-14T09:17:29.720 に答える
27

このスコープ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} - userId は、現在ログインしているユーザーの「私」だけにすることができます。

于 2014-03-17T16:23:21.230 に答える
26

私はgoogle-api-php-client のPHPバージョン 1.1.4 を使用してこれを使用して解決しました

ユーザーを Google 認証ページにリダイレクトするために次のコードが使用されているとします。

 $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

ただし、場所は返されません。新しい YouTube アカウントには YouTube 固有のユーザー名がありません

于 2015-06-10T13:15:54.417 に答える
12

これは不十分な文書です / 変更がありました。最新のエンドポイントについては、このhttps://developers.google.com/oauthplaygroundを参照してください。

2021の正しいエンドポイントの時点userinfo

https://www.googleapis.com/oauth2/v1/userinfo

だから、access_tokenあなたができることを手に入れたら

curl -X GET "https://www.googleapis.com/oauth2/v1/userinfo" \
   -H "Authorization: Bearer <access_token>"

重要:必要なすべての情報を取得するにはscopeopenid email profile.

{
 'sub': '<unique_id>',
 'name': '<full>',
 'given_name': '<first>',
 'family_name': '<last>',
 'picture': '<pic>',
 'email': '<email>',
 'email_verified': True,
 'locale': 'en'
}
于 2021-06-10T02:12:27.613 に答える
1

クライアント側の Web 環境にいる場合、新しい auth2 JavaScript API にgetBasicProfile()は、ユーザーの名前、電子メール、および画像の URL を返す非常に必要な関数が含まれています。

https://developers.google.com/identity/sign-in/web/reference#googleusergetbasicprofile

于 2015-11-02T19:10:20.327 に答える