GooglePlus 登录句柄结果

GooglePlus login handle result

在我的 Polymer-Page 上,我尝试登录到我的 google-plus 页面并检索我的姓名和封面照片 url。 为此,我使用了一个 google-signin 元素,它在登录完成时运行以下函数:

loginSuccess: function(e){
    console.log("Success!");
    gapi.client.load('plus', 'v1', function() {
        var request = gapi.client.plus.people.get( {'userId' : 'me'} );
        request.execute(function(retr){
            console.log("Data Loaded");
            console.log(retr);
            this.myFunction(retr);
        });
    });
}

最多 "Data Loaded" 一切正常,控制台还打印出 google 加的整个结果对象。但我无法执行功能 (this.myFunction) 或从那里的聚合物访问数据。如何将 gapi 请求的结果数据存储到我的聚合物变量中???

谢谢,马尔特

在 Ajax 结果中,this 上下文不同。您需要在 ajax 请求之前或您访问它的函数之前设置一个变量,如下所示:

self: this,

loginSuccess: function(e){
    console.log("Success!");
    gapi.client.load('plus', 'v1', function() {
        var request = gapi.client.plus.people.get( {'userId' : 'me'} );
        request.execute(function(retr){
            console.log("Data Loaded");
            console.log(retr);
            self.myFunction(retr);
        });
    });
}

我刚刚发现,我选择了错误的方式来获取我的个人资料数据。

有一个从google-api获取数据的聚合物元素叫做google-api-loaderhttp://googlewebcomponents.github.io/google-apis/components/google-apis/#google-api-loader)。除了 google-signin 元素之外,我还通过以下方式使用该元素:

<google-api-loader id="plus" name="plus" version="v1" on-google-api-load="{{displayProfile}}"> </google-api-loader>

这里是函数displayProfile

    displayProfile: function(){
        if (this.signedIn && this.$.plus.api) {
            var request = this.$.plus.api.people.get({"userId": "me"});
            request.execute(function (resp) {
                this.userData = resp;
            }.bind(this));
        }
    }

它非常棒,我可以通过 this.userData 访问数据。这是另一个例子:https://github.com/Scarygami/google-signin-samples/blob/master/elements/google-signin-status.html