Google登录回调-获取名称和电子邮件 [英] Google signin callback - get name and email

查看:213
本文介绍了Google登录回调-获取名称和电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在登录时将基本的Google帐户信息(名称,电子邮件地址,身份证号)捕获到数据库中.

我是通过为他们的个人资料信息设置vars并通过AJAX更新数据库来实现的.

(请参见下面示例中的vars)

if (authResult['access_token']) {
    // The user is signed in
    this.authResult = authResult;
    helper.connectServer();
    // After we load the Google+ API, render the profile data from Google+.
    //this renders profile information to the page just fine
    gapi.client.load('plus','v1',this.renderProfile);

        var email = gapi.client.load('plus','v1',this.getEmail);
        var acct_type = 'G';
        var id = gapi.client.load('plus','v1',this.getId);
        var fname = gapi.client.load('plus','v1',this.getFname);
        var lname = gapi.client.load('plus','v1',this.getLname);

    //DO AJAX!!
    $.post("_google/processlogindata.php", 
    {
        id:id,
        fname:fname,
        lname:lname,
        email:email,
        acct_type:acct_type
        });

ajax正在运行,它通过硬代码'G'并显示在数据库中.

当数据库默认值为NULL时,其余变量将显示为空白条目.

我复制了renderProfile,因为它可以在页面上显示配置文件位.例如,这就是getFname的样子(它们几乎完全相同):

     getFname: function() {
  var request = gapi.client.plus.people.get( {'userId' : 'me'} );
  request.execute( function(profile) {
     return profile.name.GivenName;
  });},

如果我在此处将返回变量设置为诸如名称"之类的静态字符串,它将显示在数据库中.因此,问题绝对是我试图访问或保存此个人资料信息的方式.

所以我的问题是,如何正确地将这些配置文件对象捕获到AJAX上方的var中?

解决方案

我用以下代码解决了这个问题:

        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
                });

            });
        });

关键是它不使用任何外部函数或变量,而是在gapi.client.load函数参数中设置了所有内容,包括变量和发送ajax.

请注意,第三个参数(函数,无论是编写的还是引用的)在Google开发人员中被列为可选",但没有它就没有什么用(返回一个诺言?),因此您想要对已加载的信息进行任何处理可能是在功能参数中完成的.出于某种原因,这种情况将其插入不同的外部函数中,最终导致我无法访问或使用其上的变量(但还要注意,在本示例中,G也传递得很好.)

我不太喜欢嵌套函数,所以弄清楚这很奇怪.

参考

Google开发人员偷偷偷走了很多示例和信息,因此以下是一些文章帮助我得出了这个答案:

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

非常模糊的gapi.client.load描述:
https://developers.google.com/api-client-library/javascript/reference/referencedocs#gapiclientload

登录资源

此页面显示了配置文件JSON返回的实时版本/代码示例: https://developers.google.com/+/web/people/

本文将向您展示每个JSON人员对象的嵌套结构: https://developers.google.com/+/api/latest/people

如果找不到它,Google会提供一个用于javascript/jQuery登录的隐藏式快速入门应用程序,该应用程序基本上可以为您完成登录,但是如果您希望它反弹到您的手机,则需要进行一些修改.其他页面,而不是用信息刷新自身.我根据自己的需要进行了科学怪人的布置. https://developers.google.com/+/quickstart/java

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 the database through AJAX.

(see vars in example below)

if (authResult['access_token']) {
    // The user is signed in
    this.authResult = authResult;
    helper.connectServer();
    // After we load the Google+ API, render the profile data from Google+.
    //this renders profile information to the page just fine
    gapi.client.load('plus','v1',this.renderProfile);

        var email = gapi.client.load('plus','v1',this.getEmail);
        var acct_type = 'G';
        var id = gapi.client.load('plus','v1',this.getId);
        var fname = gapi.client.load('plus','v1',this.getFname);
        var lname = gapi.client.load('plus','v1',this.getLname);

    //DO AJAX!!
    $.post("_google/processlogindata.php", 
    {
        id:id,
        fname:fname,
        lname:lname,
        email:email,
        acct_type:acct_type
        });

The ajax is working, it passes the hardcode 'G' and shows up in the db.

The rest of the variables appear as blank entries, when the database default is NULL.

I copied off of renderProfile since it worked for displaying profile bits on the page. This is what getFname looks like, for example (they're all pretty much the same):

     getFname: function() {
  var request = gapi.client.plus.people.get( {'userId' : 'me'} );
  request.execute( function(profile) {
     return profile.name.GivenName;
  });},

If I set the return variable here to a static string like "name", it will show up in the database. So the problem is definitely how I'm trying to access or save this profile information.

So my question is, how do I trap these profile objects correctly to the vars above the AJAX?

解决方案

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

这篇关于Google登录回调-获取名称和电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆