Google API 経由でユーザーのプロフィールから情報を取得することはできますか? 可能であれば、どの API を使用すればよいですか?
私はそのような情報に興味があります:
- ユーザー プロファイルへの URL (例: https://profiles.google.com/115063121183536852887 );
- 性別(性別);
- プロフィール写真。
また、ユーザーのプロファイルから他の情報を取得することもクールです。
Google API 経由でユーザーのプロフィールから情報を取得することはできますか? 可能であれば、どの API を使用すればよいですか?
私はそのような情報に興味があります:
また、ユーザーのプロファイルから他の情報を取得することもクールです。
これをスコープに追加します - https://www.googleapis.com/auth/userinfo.profile
認証が完了したら、https://www.googleapis.com/oauth2/v1/userinfo?alt=jsonから情報を取得します
名前、公開プロフィールの URL、性別、写真など、たくさんのものがあります。
スコープ - 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'];
このスコープ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 は、現在ログインしているユーザーの「私」だけにすることができます。
私は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 固有のユーザー名がありません
これは不十分な文書です / 変更がありました。最新のエンドポイントについては、この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>"
重要:必要なすべての情報を取得するにはscope
、openid email profile
.
{
'sub': '<unique_id>',
'name': '<full>',
'given_name': '<first>',
'family_name': '<last>',
'picture': '<pic>',
'email': '<email>',
'email_verified': True,
'locale': 'en'
}
クライアント側の Web 環境にいる場合、新しい auth2 JavaScript API にgetBasicProfile()
は、ユーザーの名前、電子メール、および画像の URL を返す非常に必要な関数が含まれています。
https://developers.google.com/identity/sign-in/web/reference#googleusergetbasicprofile