上下文
我正在尝试使用他们的 server-side/explicit flow 从 Instagram API 获取访问 token .
当用户成功验证并授权我的应用程序时,Instagram 会使用 code 参数将用户重定向到我的 redirect_uri。 获得此代码后,我将尝试调用 Instagram API 以获取 access_token。
问题
我成功获得了此代码,但为了进行此交换,我必须将代码连同一些应用程序标识参数一起发布到它们的access_token 端点:
$.ajax({
type: 'POST',
url: 'https://api.instagram.com/oauth/access_token',
// Disable credentials as they were enabled by default
xhrFields: {
withCredentials: false
},
crossDomain: true,
data: {
client_id: client_id,
client_secret: client_secret,
grant_type: 'authorization_code',
redirect_uri: callback_http,
code: token
},
}).always(function(res) {
console.log('Res from Instagram API', res);
});
问题是我遇到了 Access-Control-Allow-Origin 问题:
XMLHttpRequest cannot load https://api.instagram.com/oauth/access_token.
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin '[here is my callback_http]' is therefore not allowed access.
我试过使用 dataType: 'jsonp' 作为 Ajax 调用的参数但没有成功(401 代码)。
有什么想法吗?非常感谢您的帮助!
最佳答案
当使用服务器端显式流时,您必须使用服务器端代码进行 oauth,由于跨域请求,它会被浏览器阻止。如果你只想使用 javascript,那么使用客户端隐式流
关于javascript - Instagram API access_token 请求和 Access-Control-Allow-Origin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20382687/