2016-10-11 6 views
0

私はアプリケーションが使用するAPIを持っています。アプリケーションは、すでに認証されたユーザーまたはGoogleアカウントのid_tokenを私に送ります。APIコールを使用してid_tokenからユーザープロフィールを取得する方法

Google APIサーバーにASP.netでAPI呼び出し(Javaスクリプトコールではない)を行うと、ユーザーのプロファイル情報(名前など)を取得することができます。そのため、アプリケーションのDBにユーザーを保存できます。

私は既にクライアントIDとクライアントの秘密のGoogleプロジェクトを持っています。

答えて

0

私はasp.net APIが私が望むユーザー情報を得る方法を見つけました。

using (var client = new System.Net.WebClient()) 
    { 
     var uri = new Uri("https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=" + ACCESSTOKENFROMLOGGININ + "&access_type=offline"); 
     var response = client.DownloadString(uri); 
    } 

you response will have these fields 
{ 
"issued_to": "", 
"audience": "", 
"user_id": "", 
"scope": "", 
"expires_in": 756, 
"email": "", 
"verified_email": true, 
"access_type": "online" 
} 

Then you could make another call to get more personal information 

var userInfoUri = new Uri("https://www.googleapis.com/oauth2/v2/userinfo"); 
        var userInfoClient = new System.Net.WebClient(); 
        client.Headers.Add("Authorization", "Bearer " + YOUACCESSToken); 

and you will receive an object like this 

{ 
"id": "", 
"name": "", 
"given_name": "", 
"family_name": "", 
"link": "", 
"picture": "",`enter code here` 
"locale": "en" 
} 
関連する問題