简体   繁体   中英

401 Authorization Required on Twitter API request after logged in with auth0

I just finished auth0 angularjs tutorial described at https://auth0.com/docs/quickstart/spa/angular/no-api with a successfull twitter login (so i got token). I want to extend the seed project doing some requests to twitter API to get the user timeline, etc.

I've tried it doing $http request with jsonp and adding a request interceptor at app.js with the Authorization header. It looks like this:

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;
        }
    };
})

Response returns 401 Authorization Required:

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

What am i doing wrong? Should I make another thing before do request to get timeline? Maybe am i confused about oauth works and i need access_token to do the request?

Thanks in advance.

When calling the Twitter API, you must call it with the Twitter access token and not with the Auth0 token which is for your API.

Check this docs which explains how to do it: https://auth0.com/docs/what-to-do-once-the-user-is-logged-in/calling-an-external-idp-api

Let me know if this helps :).

Cheers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM