Google Plus API错误gapi.loaded_0
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Google Plus API错误gapi.loaded_0相关的知识,希望对你有一定的参考价值。
我正在尝试将requireJS与Google plus API一起使用,但是当我点击登录按钮时出现错误
这是错误和截图:
GET
https://apis.google.com/_/scs/apps-static/_/js/k=oz.gapi.en.f5Li87Zolqg.O/m…sv=1/d=1/ed=1/am=AQ/RS=AGLTcCPR6xH_GlLdKZ8KMxaWNPWQokoYsg/cb=gapi.loaded_0
屏幕截图:https://i.imgsafe.org/098c5b5634.png
我正在使用此代码: gapi is not defined error comes when using with requirejs
注意:我在javascript文件中调用gp.js
并在那里定义它是第一行。
我的screen.js文件
define(["facebook","fb","googleplus","gp"], function(facebook,fb,gapi,gp){
// some bunch other code will go here
$('#login').click(login);
// some other code will go here
});
gp.js包含以下代码
//$('#login').click(login);
//$('#logout').click(logout);
function logout(){
gapi.auth.signOut();
location.reload();
}
function login() {
var myParams = {
'clientid' : '455646565646-ppqmgsfghfdhgfghqguj3i4ir70i.apps.googleusercontent.com',
'cookiepolicy' : 'single_host_origin',
'callback' : 'loginCallback',
'approvalprompt':'force',
'scope' : 'https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/plus.me https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile'
};
gapi.auth.signIn(myParams);
}
window.loginCallback = function (result) {
if (result['status']['signed_in']) {
gapi.client.load('plus', 'v1', function () {
var request = gapi.client.plus.people.get(
{
'userId': 'me'
});
request.execute(function (resp) {
var email = '';
if (resp['emails']) {
for (i = 0; i < resp['emails'].length; i++) {
if (resp['emails'][i]['type'] == 'account') {
email = resp['emails'][i]['value'];
}
}
}
var str = "Name:" + resp['displayName'] + "<br>";
str += "Email:" + email + "<br>";
str += "DOB:" + resp['birthday'] + "<br>";
str += "Gender:" + resp['gender'] + "<br>";
document.getElementById("profile").innerhtml = str;
});
});
}
}
onLoadCallback = function(){
gapi.client.setApiKey('Sgtfjhygjhgjhg9U1nKaZ5H1MmwTuthspQPNqY');
gapi.client.load('plus', 'v1',function(){});
}
我的main.js就在这里
require.config({
shim: {
'gp' : {
deps: ['jquery','googleplus'],
},
'googleplus' : {
deps: ['jquery'],
exports: 'gapi'
},
},
paths: {
'googleplus': 'https://apis.google.com/js/client.js?onload=onLoadCallback'
},
})
require(['gp']);
为什么会发生这种情况,我该如何解决?
gs.js必须是一个模块,如果你想使用模块内定义的函数,这个模块必须返回这个函数:
define(['jquery', 'googleplus'], function($, gapi) {
function logout(){
gapi.auth.signOut();
location.reload();
}
function login() {
var myParams = {
'clientid' : '455646565646-ppqmgsfghfdhgfghqguj3i4ir70i.apps.googleusercontent.com',
'cookiepolicy' : 'single_host_origin',
'scope' : 'https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/plus.me https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile'
};
gapi.load('client:auth2', function() {
gapi.auth2.init(myParams).then(function() {
gapi.auth2.getAuthInstance().signIn().then(loginCallback);
});
});
}
var loginCallback = function () {
gapi.client.load('plus', 'v1', function () {
var request = gapi.client.plus.people.get(
{
'userId': 'me'
});
request.execute(function (resp) {
var email = '';
if (resp['emails']) {
for (i = 0; i < resp['emails'].length; i++) {
if (resp['emails'][i]['type'] == 'account') {
email = resp['emails'][i]['value'];
}
}
}
var str = "Name:" + resp['displayName'] + "<br>";
str += "Email:" + email + "<br>";
str += "DOB:" + resp['birthday'] + "<br>";
str += "Gender:" + resp['gender'] + "<br>";
document.getElementById("profile").innerHTML = str;
});
});
}
gapi.client.setApiKey('Sgtfjhygjhgjhg9U1nKaZ5H1MmwTuthspQPNqY');
return {
login: login,
logout: logout
};
});
现在您可以在其他模块中使用login
和logout
函数:
define(["jquery", "gp"], function($, gp){
$('#login').click(gp.login);
});
编辑:
我尝试基于login
重写https://developers.google.com/identity/sign-in/web/reference#gapiauth2initwzxhzdk19paramswzxhzdk20函数
以上是关于Google Plus API错误gapi.loaded_0的主要内容,如果未能解决你的问题,请参考以下文章
无法使用 Google Plus 登录,出现 com.google.android.gms.common.api.ApiException: 10:
使用 google+ API 从 google plus 社区和活动获取数据
是否有用于 Google Plus Collections 的 API?