Google signin callback - get name and email

前端 未结 1 1492
一整个雨季
一整个雨季 2021-01-26 17:46

I am trying to trap basic google account information (name, email, id.) into a database on sign in.

I am doing this by setting vars for their profile info and updating t

1条回答
  •  有刺的猬
    2021-01-26 18:24

    I solved this with the following code:

            var acct_type = 'G';
    
            gapi.client.load('plus','v1', function(){
    
                var request = gapi.client.plus.people.get({
                  'userId': 'me'
                });
    
                request.execute(function(resp) {
                    var id = resp.id;
                    var fname = resp.name.givenName;
                    var lname = resp.name.familyName;
                    var email = resp.emails[0].value;
    
                    $.post("_yourDBinjection_.php", 
                    {
                    id:id,
                    fname:fname,
                    lname:lname,
                    email:email,
                    acct_type:acct_type
                    });
    
                });
            });
    

    The point being that it uses no external functions or variables, it sets everything in the gapi.client.load function parameter including the variables and sending the ajax.

    Note that the third parameter (a function, whether written or referenced) is listed on Google dev as 'optional' but it's not very useful without it (returns a promise?) So anything you want to do with the loaded information should probably be done in the function parameter. For some reason with this case piecing it out in different external functions I ended up not being able to reach or use variables above it (but note also that G passed just fine in this example as well.)

    I don't like nested functions much so figuring this out was weird.

    REFERENCES

    Google dev sneaks away a lot of examples and info so here are some articles that helped me generate this answer:

    (javascript version)
    https://developers.google.com/+/api/latest/people/get#examples

    the very vague gapi.client.load description:
    https://developers.google.com/api-client-library/javascript/reference/referencedocs#gapiclientload

    RESOURCES FOR SIGN-IN

    this page shows a live version/code example of profile JSON return: https://developers.google.com/+/web/people/

    this article will show you the nesting structure of each JSON people object: https://developers.google.com/+/api/latest/people

    if you couldn't find it, Google has a well-hidden quickstart app for javascript/jQuery sign-in that basically does the sign-in work for you, except it takes some ripping up if you want it to bounce to other pages rather than refresh itself with information. I frankensteined it to my needs. https://developers.google.com/+/quickstart/java

    0 讨论(0)
提交回复
热议问题