我正在尝试像这样使用 Google plus API(通过 googie-api-javascript)实现(省略完整代码):
var clientId = '7454475891XxxxxxXom4c6n.apps.googleusercontent.com'; //fake client
var apiKey = '-uTH_p6NokbrXXXXXXXXXXXXX'; //Fake Key
var scopes = 'https://www.googleapis.com/auth/plus.me';
function handleClientLoad() {
gapi.client.setApiKey(apiKey);
window.setTimeout(checkAuth,1);
}
function checkAuth() {
gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: true}, handleAuthResult);
}
function handleAuthResult(authResult) {
if (authResult && !authResult.error) {
makeApiCall();
} else {
//handle user-approval
}
}
// Load the API and make an API call. Display the results on the screen.
function makeApiCall() {
gapi.client.load('plus', 'v1', function() {
var o = gapi.client.plus;
alert(o);
});
}
代码在 gapi.client.load(包括允许访问的用户)之前运行良好 - 此回调被调用但 alert(o) 将返回未定义。
在检查 HTTP 请求后,我看到 .load 发出了一个请求:
https://content.googleapis.com/discovery/v1/apis/plus/v1/rpc?fields=methods%2F*%2Fid&pp=0&key=-uTH_p6NokbrXXXXXXXX
这将返回带有以下消息的 HTTP 400:
{"error":{"errors":[{"domain":"usageLimits","reason":"keyInvalid","message":"Bad Request"}],"code":400,"message":"Bad Request"}}
我的问题是 - 我需要更改什么才能使这项工作正常进行? 我需要启用一些 secret 设置吗? Google+ 在 API 列表下的 google-developer-console 中启用。
感谢您的帮助, 阿龙
最佳答案
问题: .load 向谷歌发现服务发出加载 .JS 的请求。如果服务收到的请求包含 api key ,该服务将出错。 (不知道为什么库会这样,好像是个bug?)
修复:
gapi.client.setApiKey(""); //NEW
gapi.client.load('plus', 'v1', function()
//re-add the key later if you need it
From Discovery Service docs: requests you make to the Discovery Service API should not include an API key. If you do provide a key, the requests will fail.
奇怪...:P
关于javascript - Google Javascript API (gapi) - .load 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20785327/