How to call the API using OAuth 1.0?

2019-08-06 05:25发布

I'm having a bad time calling the API after successfully retrieving the user's access_token and his secret_token, like so:

{ 
    oauth_token: '4xxxxxxxx-asdasdasd......',
    oauth_token_secret: 'XXX........',
    user_id: '4xxxxxxxx' 
}

I retrieved those in a user sign-in in behalf of my application. For example, like Twitter sign-in implementation.

Now for retrieving data from an API I am not sure about the specification for OAuth 1.0, but I know in OAuth 2.0 you can simply use 'Authorization: Bearer ' . $accessToken in the header. Is OAuth 1.0 requires generating another signature for each API call?

I Have searched in this matter and did not found a clear solution. Thanks before

1条回答
Melony?
2楼-- · 2019-08-06 05:30

Generally for OAuth1.0 the Authorization header is sent with a value of OAuth and a bunch of fields. There is a spec about it, and also a signature is generated and added to that string, but you don't have to do that yourself.

You can use the request module. For example here is how you can get the user's profile for Twitter:

request.get('https://api.twitter.com/1.1/users/show.json', {
  oauth:{
    consumer_key:'...',
    consumer_secret:'...',
    token:'...',
    token_secret:'...'
  },
  qs:{user_id:'...'} // or screen_name
}, function (err, res, body) {})

You can use the above code with any OAuth1.0 provider. All you need to pass is your application and user credentials. In very rare cases you might need to pass additional options (check out the oauth signing section).

查看更多
登录 后发表回答