2017-08-18 5 views
2

私は、クライアントがGoogleアカウントとFacebookアカウントを使用してサインアップ/ログインを許可するオプションを求めているウェブサイトで働いています。ユーザーのGoogleプロフィールからデータベースに保存するためにメールアドレスを抽出するにはどうすればよいですか?Googleアカウントからメールアドレスを抽出する

ここに私のコードです。問題は、ユーザープロファイルを完全に取得できないことです。代わりに、私はちょうどユーザー名を受け取ります。

try 
{ 
    WebClient client = new WebClient(); 
    var urlProfile = "https://www.googleapis.com/oauth2/v1/userinfo?access_token=" 
     + access_token; 

    string outputData = client.DownloadString(urlProfile); 

    GoogleUserOutputData serStatus = 
     JsonConvert.DeserializeObject<GoogleUserOutputData>(outputData); 

    if (serStatus != null) 
    { 
     return serStatus; 
     // You will get the user information here. 
    } 
} 
catch (Exception ex) 
{ 
    //catching the exception 
} 

return null; 

答えて

1

JavaScriptでデータ(メールなど)を受信するための方法です。最後に、データにアラートが表示されます。 (このデータをデータベースに保存することができます)。これは、Googleのボタンを使った完全な実例です。このリンク読んで(必要がある)ことができます詳細情報および詳細については

<html> 

    <head> 
    <title>Demo: Getting an email address using the Google+ Sign-in button</title> 
    <!-- Include the API client and Google+ client. --> 
    <script src = "https://plus.google.com/js/client:platform.js" async defer></script> 
    </head> 

    <body> 
    <!-- Container with the Sign-In button. --> 
    <div id="gConnect" class="button"> 
     <button class="g-signin" 
      data-scope="email" 
      data-clientid="Your_Client_ID" 
      data-callback="onSignInCallback" 
      data-theme="dark" 
      data-cookiepolicy="single_host_origin"> 
     </button> 
     <!-- Textarea for outputting data --> 
     <div id="response" class="hide"> 
     <textarea id="responseContainer" style="width:100%; height:150px"></textarea> 
     </div> 
    </div> 
</body> 

    <script> 
    /** 
    * Handler for the signin callback triggered after the user selects an account. 
    */ 
     function onSignInCallback(resp) { 

    gapi.client.load('plus', 'v1', apiClientLoaded); 
    } 

    /** 
    * Sets up an API call after the Google API client loads. 
    */ 
     function apiClientLoaded() { 

    gapi.client.plus.people.get({userId: 'me'}).execute(handleEmailResponse); 
    } 

    /** 
    * Response callback for when the API client receives a response. 
    * 
    * @param resp The API response object with the user email and profile information. 
    */ 
    function handleEmailResponse(resp) { 
     var primaryEmail; 
     var name; 
     var gender; 

    for (var i=0; i < resp.emails.length; i++) { 
     if (resp.emails[i].type === 'account') 
      primaryEmail = resp.emails[i].value; 
     if (resp.displayName != null) 
      name = resp.displayName; 
     gender = resp.gender; 
    } 
    document.getElementById('responseContainer').value = 'Primary email: ' + 
      primaryEmail + '\n\nFull Response:\n' + JSON.stringify(resp); 
     ShowAlert("Email: "+primaryEmail +" "+"Name: "+ resp.displayName +" "+"Gender: "+gender); 
    } 

    </script> 

</html> 

Getting people and profile information

+1

ありがとうございました。しかし、まだ1つのエラーが発生していましたが、応答が得られませんでした。ダッシュボードに「Google+ APIを有効にする」というソリューションがありました。誰かがこの問題に直面している場合は、Googleの開発者アカウントでGoogle+ APIを有効にしてください。 https://console.developers.google.com/projectselector/apis/library?pli=1 – MuhammadMohsan

+0

@MuhammadMohsan私はVinod kumar Gの答えにあなたの機能強化を受け入れました。しかし、一般的に、これを行うべきではありません。代わりに、自分の*回答を投稿する必要があります。また、礼儀正しく編集した後、承認しなかった場合には、投稿を投稿した人に編集をロールバックするよう求める回答をコメントに残すことができます。しかし、非常に頻繁にこれをしないでください。 – DavidRR

1

ドキュメントはキーです。完全に確認してください。ここで

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

<meta name="google-signin-client_id" content="YOUR_CLIENT_ID.apps.googleusercontent.com"> 
<div class="g-signin2" data-onsuccess="onSignIn"></div> 
function onSignIn(googleUser) { 
    var profile = googleUser.getBasicProfile(); 
    console.log('ID: ' + profile.getId()); // Do not send to your backend! Use an ID token instead. 
    console.log('Name: ' + profile.getName()); 
    console.log('Image URL: ' + profile.getImageUrl()); 
    console.log('Email: ' + profile.getEmail()); // This is null if the 'email' scope is not present. 
} 
<a href="#" onclick="signOut();">Sign out</a> 

<script> 
function signOut() { 
    var auth2 = gapi.auth2.getAuthInstance(); 
    auth2.signOut().then(function() { 
     console.log('User signed out.'); 
    }); 
    } 
</script> 
+0

を私は@MuhammadMohsanによって強化を受け入れました。あなたがそれを承認しない場合、編集をロールバックしてください。 – DavidRR

関連する問題