我使用ngResource在Amazon Web Services上调用REST API时收到此错误:
XMLHttpRequest无法加载 http://server.apiurl.com:8000/s/login?login=facebook.对预检请求的响应未通过访问控制检查:请求的资源上不存在"Access-Control-Allow-Origin"标头.原产地" :HTTP //本地主机,因此"是不允许访问. 错误405
服务:
socialMarkt.factory('loginService', ['$resource', function($resource){
var apiAddress = "http://server.apiurl.com:8000/s/login/";
return $resource(apiAddress, { login:"facebook", access_token: "@access_token" ,facebook_id: "@facebook_id" }, {
getUser: {method:'POST'}
});
}]);
Run Code Online (Sandbox Code Playgroud)
控制器:
[...]
loginService.getUser(JSON.stringify(fbObj)),
function(data){
console.log(data);
},
function(result) {
console.error('Error', result.status);
}
[...]
Run Code Online (Sandbox Code Playgroud)
我正在使用Chrome,我不知道还有什么可以解决这个问题.我甚至将服务器配置为接受来自源的头文件localhost.
当且仅当用户在发出OAuth2请求时登录LinkedIn时,它才有效.
如果用户未登录,则会遇到错误.
我们行动的顺序:
https://api.linkedin.com/v1/people/\~在此之后,我们收到401以下内容:
{
"errorCode": 0,
"message": "Unable to verify access token",
"requestId": "C0DUCX81SA",
"status": 401,
"timestamp": 1421946470523
}
Run Code Online (Sandbox Code Playgroud)
有时,经过一段时间后,使用相同的访问令牌重试会产生200.有时不会.
如果用户在此"401周期"期间登录到LinkedIn,则神奇地以前获取的访问令牌开始工作.
我不知道如何解决这个问题,因为它似乎是LinkedIn的一个问题.
有人有任何建议或者之前有人看过这种行为吗?
我们尝试过cookie设置,在请求之前等待一段时间.
我们正在向Zotonic [1]添加LinkedIn OAuth2身份验证,但现在仍然使用非工作模块.
编辑:
有人提到LinkedIn的两个讨论.他的答复现在遗憾地从下面的讨论中消失了.
这些是链接:
https://developer.linkedin.com/forum/unable-verify-access-token
我在这些讨论中尝试了所有建议,但无济于事.
编辑#2:
检查LinkedIn上的第一个讨论表明我并不是唯一一个遇到这些一致性问题的人.如果用户已经清除了cookie或者在OAuth"舞蹈"期间必须登录LinkedIn,那么在LinkedIn上会出现问题 https://developer.linkedin.com/forum/unable-verify-access-token#comment-36950
更新
解决了,感谢Matthijs Bierman,请看下面的答案.
我正在尝试生成访问令牌来收集linkedin数据.我按照linkedin API文档中提供的说明进行操作.我在开发者页面中创建了一个应用程序并获得以下内容
Application Details
• Company:
Fresher
• Application Name:
xxxxxxxxxx
• API Key:
75pcum6zb2cael
• Secret Key:
xxxxxxxxxxxxxxxx
• OAuth User Token:
xxxx-xxxx-xxxx-xxxx-xxxxxxxxxx
• OAuth User Secret:
xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx
Run Code Online (Sandbox Code Playgroud)
使用API密钥我使用URL 生成authorization_code:
但是当我最终尝试使用以下URL生成访问令牌时,我收到了错误响应:
{"error_description":"缺少必需参数,包括无效参数值,参数多次.:无法检索访问令牌:appId或重定向uri与授权代码或授权代码过期不匹配","错误":"invalid_request" }
即使经过多次验证,也会出现相同的错误消息.
请帮忙.谢谢.
我尝试使用LinkedIn共享API通过AngularJS应用程序中的JavaScript发布新内容,如下所示.
var xml = "<share><comment>" + content + "</comment><visibility><code>anyone</code></visibility></share>";
var req = {
method: 'POST',
url: 'https://api.linkedin.com/v1/people/~/shares?oauth2_access_token=' + account.token,
headers: {
'Content-Type': 'text/plain'
},
data: xml
};
$http(req).success(function(data) {
console.log(data);
console.log('published to linkedin');
}).error(function() {
console.log(arguments);
console.log('failed to publish to linkedin');
});
Run Code Online (Sandbox Code Playgroud)
我可以成功发布这些数据.但是,浏览器会阻止响应被读取,因为响应没有"Access-Control-Allow-Origin"标头.

但是,我已经在LinkedIn应用程序设置中提供了http://localhost:3000" https:// localhost:3000 "域名.

Chrome中的请求/响应如下所示.

有关如何阅读响应而不让浏览器阻止响应的任何想法?
我认为问题是Access-Control-Allow-OriginLinkedIn API响应中缺少标题?