gpt4 book ai didi

AngularJS - 如何通过 Oauth2 连接到 Twitter 仅应用程序身份验证?

转载 作者:行者123 更新时间:2023-12-02 22:20:23 26 4
gpt4 key购买 nike

我尝试从 Twitter application-only authentication 接收 accessToken但我不断收到来自 Twitter api 的 405(方法不允许)响应。有人知道如何解决这个问题吗?我被困住了..

我知道以下事实:
- 最佳实践是从服务器端执行此操作,但我想在客户端使用 Angular 进行尝试
- X-Requested-With 应从 header 中删除

这是我创建的工厂:

twitterServices.factory('Connect', function($http){
var factory = {};
var baseUrl = 'https://api.twitter.com/';

var bearerToken = function(){
var consumerKey = encodeURIComponent('****');
var consumerSecret = encodeURIComponent('****');
var tokenCredentials = btoa(consumerKey + ':' + consumerSecret);

return tokenCredentials;
};

factory.fetchAccessToken = function(scope){
var oAuthurl = baseUrl + "oauth2/token";
var headers = {
'Authorization': 'Basic ' + bearerToken(),
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
};
$http.defaults.useXDomain = true;
delete $http.defaults.headers.common['X-Requested-With'];
$http({method: 'POST', url: oAuthurl, headers: headers, data: 'grant_type=client_credentials'}).
success(function(data, status){
scope.status = status;
scope.data = data;
}).
error(function(data, status){
scope.status = status;
scope.data = data || "Request failed";
});
};

factory.fetchTimeLine = function(scope){
scope.fetchAccessToken();
//the rest
};
return factory;
});

这是 Chrome 中的 header 请求/响应:

Request URL:`https://api.twitter.com/oauth2/token`
Request Method:OPTIONS
Status Code:405 Method Not Allowed
Request Headersview source
:host:api.twitter.com
:method:OPTIONS
:path:/oauth2/token
:scheme:https
:version:HTTP/1.1
accept:*/*
accept-encoding:gzip,deflate,sdch
accept-language:en-US,en;q=0.8
access-control-request-headers:accept, authorization, content-type
access-control-request-method:POST
origin:`http://localhost`
referer:`http://localhost/test/app/
user-agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.69 Safari/537.36

Response Headersview source
content-length:0
status:405 Method Not Allowed
version:HTTP/1.1

我的控制台显示以下内容:

OPTIONS https://api.twitter.com/oauth2/token 405 (Method Not Allowed) angular.js:9312
OPTIONS https://api.twitter.com/oauth2/token Origin http://localhost is not allowed by Access-Control-Allow-Origin. angular.js:9312
XMLHttpRequest cannot load https://api.twitter.com/oauth2/token. Origin http://localhost is not allowed by Access-Control-Allow-Origin. (index):1

最佳答案

关于AngularJS - 如何通过 Oauth2 连接到 Twitter 仅应用程序身份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19552581/

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