2017-11-08 19 views
0

Google People APIを使用してログインしたユーザーのメールアドレスを取得するにはどうすればよいですか?Google People APIを使用してログインしたユーザーのメールを取得する

ログインしたユーザーのメールアドレスを取得しようとしていますので、アプリでデータと比較することができます。私はそれを得るために様々なことを試みましたが、以下のコードはちょうどundefinedを返しています。

 function makeApiCall() { 
    gapi.client.people.people.get({ 
     'resourceName': 'people/me', 
     'requestMask.includeField': 'person.names,person.emailAddresses' 
    }).then(function(resp) { 
     var name = resp.result.names[0].givenName; 
     var email = resp.result.emailAddresses[0].emailAddress; 
     authorizeButton.insertAdjacentHTML('beforebegin', '<small rel="' + email + '">Logged in as ' + name + '</small>'); 
    }); 
    } 

[object Object]var email = resp.result.emailAddresses[0];結果にそれを変更します。

私もJSON.parse()を使用してみましたが、次のエラーを得た:

Uncaught SyntaxError: Unexpected token u in JSON at position 0 
at JSON.parse (<anonymous>) 
at (index):285 
at h.r2 (cb=gapi.loaded_0:119) 
at xs (cb=gapi.loaded_0:122) 
at Wq (cb=gapi.loaded_0:122) 
at _.C.uea (cb=gapi.loaded_0:121) 
at Ap (cb=gapi.loaded_0:115) 
at <anonymous> 

は、私はまたthis other unanswered question about the Google People APIのメソッドを試してみました。

+1

変更、それを使用することもできます。 –

答えて

2

一度ここで

console.log(response.result); 

を行ってください。その今までに該当する正しいオブジェクトを使用した後

function makeApiCall() { 
    gapi.client.people.people.get({ 
     'resourceName': 'people/me', 
     'requestMask.includeField': 'person.names,person.emailAddresses' 
    }).then(function(resp) { 

     console.log(response.result); 
     var name = resp.result.names[0].givenName; 
     //var email = resp.result.emailAddresses[0].emailAddress; 
     //authorizeButton.insertAdjacentHTML('beforebegin', '<small rel="' + email + '">Logged in as ' + name + '</small>'); 
    }); 
    } 

を。

あなたは、[オブジェクトのオブジェクト]とコンソールがしてください電子メールのオブジェクトを記録し、ログを共有してもらう設定に戻す

var email = resp.result.emailAddresses[0].value; 
+0

非常に感謝して、それは非常に便利です。 'resp.result.emailAddresses [0] .value;'は正しく、 'console.log(resp.result);'も本当に便利でした。 –

+0

ここにはうれしい!答えを受け入れてくれてありがとう –

関連する問題