gpt4 book ai didi

javascript - 在 gwt 项目中使用 google plus api 时,@me 被匿名错误调用

转载 作者:行者123 更新时间:2023-11-28 09:56:25 25 4
gpt4 key购买 nike

我正在使用google api javascript client获取有关 Google 应用引擎中托管的 gwt 项目内的用户个人资料的信息。

在本地主机中,数据被正确检索。我通过 google plus 配置文件获得了 json。当我部署到 appengine 时,响应是 401,“@me 由匿名调用”。

这是我的代码:

<script src="https://apis.google.com/js/client.js"></script>
<script type="text/javascript">
$(function() {
auth();
});

var API_KEY = "***************************************";
var CLIENT_ID = "************.apps.googleusercontent.com";

var scopes = 'https://www.googleapis.com/auth/plus.me';

function auth() {
var config = {
'client_id' : CLIENT_ID,
'scope' : scopes,
'key' : API_KEY,
};
gapi.client.load('plus', 'v1', function() {
api.client.setApiKey(API_KEY);
gapi.auth.authorize(config, function() {
var request = gapi.client.plus.people.get({
'userId' : 'me',
});
request.execute(function(resp) {
console.log(resp);
});
});
});
}

</script>

我尝试过:

  • 在开始时调用 api.client.setApiKey。
  • 使用 Google API 控制台创建新的 Google API 访问
<小时/>

更新:

这是完整的响应错误消息:

{
"error": {
"code": 401,
"message": "me called by anonymous",
"data": [
{
"domain": "global",
"reason": "authError",
"message": "me called by anonymous",
"locationType": "header",
"location": "Authorization"
}
]
},
"id": "gapiRpc"
}

还有其他可能相关的消息:

这是其中之一:

Skipping duplicate osapi method definition chili.people.list on transport googleapis; others may exist, but suppressing warnings cb=gapi.loaded1 (línea 119)
Skipping duplicate osapi method definition pos.plusones.list on transport googleapis; others may exist, but suppressing warnings cb=gapi.loaded1 (línea 119)
Skipping duplicate osapi method definition chili.activities.list on transport googleapis; others may exist, but suppressing warnings cb=gapi.loaded1 (línea 119)
Skipping duplicate osapi method definition googleapis.newHttpRequest on transport googleapis; others may exist, but suppressing warnings

这是另一个:

Invalid auth token. 1025***** vs 140186****

最佳答案

我最终可以通过以下设置或步骤解决该问题:

1) 在 google apis 控制台中,我将重定向 URI 部分留空,并使用我网站的 URL 完成 JavaScript 起源 部分,并使用 <强>https协议(protocol):

JavaScript origins:
http://example.com
https://example.com

我将加载 api 的脚本放在结束正文标记之前:

<script src="https://apis.google.com/js/client.js"></script>

该脚本位于正文内部,位于 api 脚本之前:

<script type="text/javascript">

var API_KEY = "***************************************";
var CLIENT_ID = "************.apps.googleusercontent.com";

var scopes = 'https://www.googleapis.com/auth/plus.me';

function auth() {
var scopes = 'https://www.googleapis.com/auth/plus.me';

gapi.client.setApiKey(API_KEY);
window.setTimeout(checkAuth, 1000);

function checkAuth() {
gapi.auth.authorize({
client_id : CLIENT_ID,
scope : scopes,
immediate : false
}, handleAuthResult);
}

function handleAuthResult(authResult) {
if (authResult) {
makeApiCall();
} else {
checkAuth();
}
}

function makeApiCall() {
gapi.client.load('plus', 'v1', function() {
var request = gapi.client.plus.people.get({
'userId' : 'me'
});
request.execute(function(resp) {
$("#image").attr("src", resp.image.url);
});
});
}

}
</script>

然后,当用户单击查看他的图片时,我调用函数 auth()。

关于javascript - 在 gwt 项目中使用 google plus api 时,@me 被匿名错误调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9952715/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com