开发者

Getting user profile with google oauth2

开发者 https://www.devze.com 2023-04-09 16:22 出处:网络
I\'m trying to get user profile information upon logging in with google-oauth2. User successfully logs in and i can get the access_token and can refresh the token when needed.

I'm trying to get user profile information upon logging in with google-oauth2. User successfully logs in and i can get the access_token and can refresh the token when needed. Though i could not manage to get any information about the user despite reading the docs and trying for hours.

From "Retrieving profiles" section of developers guide :

https://www.google.com/m8/feeds/profiles/domain/domainName/full

should be enough. i've tried with开发者_JS百科 "gmail.com", "google.com", "gmail", "google", "orkut", "orkut.com" , myregisteredappsdomainname (and .com) as domainName. i've also tried it with

https://www.google.com/m8/feeds/profiles/domain/domainName/full?access_token=access_token_for_user

all i managed to get was 401 error, where it says "That’s an error.". Regarding 401 error, I've refreshed the token and tried again with new token, but kept getting 401s.

How can i get profile information and image address for user upon logging in?


The scope you're looking for is:

https://www.googleapis.com/oauth2/v1/userinfo

This has been already answered here


I was getting similar errors requesting profiles even after correctly defining the scope and getting access tokens etc.. The trick for me was to include the API version on my requests. See here for more info http://code.google.com/googleapps/domain/profiles/developers_guide.html#Versioning


Maybe little late yet could this be helpful to someone. Below is the working code I wrote to get gplus user profile

In HTML below markup will display goolge signIn button

<span id="signinButton">
    <span
      class="g-signin"
      data-callback="signinCallback"
      data-clientid="YOUR GPLUS CLIENT ID"
      data-cookiepolicy="single_host_origin"
      data-scope="email">
    </span>
</span>   

Below is the java script

 var access_token;
/**
 * Called when the Google+ client library reports authorization status.
 */
function signinCallback(authResult) {
    access_token = authResult.access_token;

    gapi.client.load('plus', 'v1', function () {
        gapi.client.plus.people.get({ userId: 'me' }).execute(printProfile);
    });
}

/**
 * Response callback for when the API client receives a response.
 *
 * @param resp The API response object with the user email and profile information.
 */
function printProfile(resp) {
    if (resp.code != 403) {
     console.log('name:' + access_token.givenname);
     console.log('last name:' + access_token.lastname);
     console.log('email:' + access_token.emails[0]);
     console.log('gender:' + access_token.gender);
     console.log('profile image url:' + access_token.image.url);
    }
}

Please make sure that you load google api javascript asynchronously within the body tag as below

            <script type="text/javascript">
                (function () {
                    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
                    po.src = 'https://apis.google.com/js/platform.js';
                    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
                })();
</script>

To handle logout refer to the answer I provide in below link, you will need to store access_token in backend so that during logout call this to be used, in my case I have stored in session and getting through ajax call gapi.auth.signOut(); not working I'm lost


Hey why don't you look at the code given at: http://www.codeproject.com/KB/aspnet/OAuth4Client.aspx

It definitely helps you. The project is actually an oauth playground to send correct oauth header to correct endpoints.

0

精彩评论

暂无评论...
验证码 换一张
取 消