使用 auth0 登录后,Twitter API 请求需要 401 授权
401 Authorization Required on Twitter API request after logged in with auth0
我刚刚完成 https://auth0.com/docs/quickstart/spa/angular/no-api 中描述的 auth0 angularjs 教程并成功登录 Twitter(所以我得到了令牌)。我想扩展种子项目,向 Twitter API 发送一些请求以获取用户时间线等
我试过使用 jsonp 执行 $http
请求,并在 app.js 处使用授权 header 添加请求拦截器。它看起来像这样:
HomeCtrl
$http.jsonp('https://api.twitter.com/1.1/statuses/home_timeline.json?callback=JSON_CALLBACK')
.then(function(data) {
console.log(data);
});
App.js
.config( function myAppConfig ($routeProvider, authProvider, $httpProvider, $locationProvider, jwtInterceptorProvider, RestangularProvider) {
...
$httpProvider.interceptors.push('jwtInterceptor');
})
.factory('jwtInterceptor', function(store) {
return {
request: function(config) {
config.headers = config.headers || {};
config.headers.Authorization = 'Bearer ' + store.get('token');
console.log(config);
return config;
}
};
})
响应returns 401 需要授权:
Failed to load resource: the server responded with a status of 401 (Authorization Required)
https://api.twitter.com/1.1/statuses/home_timeline.json?callback=angular.callbacks._0
我做错了什么?在请求获取时间表之前我应该做另一件事吗?也许我对 oauth 的工作感到困惑,我需要 access_token 来完成请求?
提前致谢。
调用 Twitter API 时,您必须使用 Twitter 访问令牌而不是用于您的 API 的 Auth0 令牌来调用它。
查看此文档,其中说明了如何操作:https://auth0.com/docs/what-to-do-once-the-user-is-logged-in/calling-an-external-idp-api
如果这有帮助请告诉我:)。
干杯!
我刚刚完成 https://auth0.com/docs/quickstart/spa/angular/no-api 中描述的 auth0 angularjs 教程并成功登录 Twitter(所以我得到了令牌)。我想扩展种子项目,向 Twitter API 发送一些请求以获取用户时间线等
我试过使用 jsonp 执行 $http
请求,并在 app.js 处使用授权 header 添加请求拦截器。它看起来像这样:
HomeCtrl
$http.jsonp('https://api.twitter.com/1.1/statuses/home_timeline.json?callback=JSON_CALLBACK')
.then(function(data) {
console.log(data);
});
App.js
.config( function myAppConfig ($routeProvider, authProvider, $httpProvider, $locationProvider, jwtInterceptorProvider, RestangularProvider) {
...
$httpProvider.interceptors.push('jwtInterceptor');
})
.factory('jwtInterceptor', function(store) {
return {
request: function(config) {
config.headers = config.headers || {};
config.headers.Authorization = 'Bearer ' + store.get('token');
console.log(config);
return config;
}
};
})
响应returns 401 需要授权:
Failed to load resource: the server responded with a status of 401 (Authorization Required)
https://api.twitter.com/1.1/statuses/home_timeline.json?callback=angular.callbacks._0
我做错了什么?在请求获取时间表之前我应该做另一件事吗?也许我对 oauth 的工作感到困惑,我需要 access_token 来完成请求?
提前致谢。
调用 Twitter API 时,您必须使用 Twitter 访问令牌而不是用于您的 API 的 Auth0 令牌来调用它。
查看此文档,其中说明了如何操作:https://auth0.com/docs/what-to-do-once-the-user-is-logged-in/calling-an-external-idp-api
如果这有帮助请告诉我:)。
干杯!