javascript - Extract email address from Google account -
i'm working on website client has asked option allow signup/login using google , facebook accounts. how can extract email address user's google profile storing in database?
here code. problem not getting user profile completely. instead, receiving user name.
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; // user information here. } } catch (exception ex) { //catching exception } return null;
here way receive data (email, etc.) in javascript. @ end shows alert data. (you can store data in database.) it's complete working example google button.
<html> <head> <title>demo: getting email address using google+ sign-in button</title> <!-- include api client , google+ client. --> <script src = "https://plus.google.com/js/client:platform.js" async defer></script> </head> <body> <!-- container 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 outputting data --> <div id="response" class="hide"> <textarea id="responsecontainer" style="width:100%; height:150px"></textarea> </div> </div> </body> <script> /** * handler signin callback triggered after user selects account. */ function onsignincallback(resp) { gapi.client.load('plus', 'v1', apiclientloaded); } /** * sets api call after google api client loads. */ function apiclientloaded() { gapi.client.plus.people.get({userid: 'me'}).execute(handleemailresponse); } /** * response callback when api client receives response. * * @param resp api response object user email , profile information. */ function handleemailresponse(resp) { var primaryemail; var name; var gender; (var i=0; < 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>
for further information , detail can (should) read link: getting people , profile information
Comments
Post a Comment