6

I am trying to get user details after user login from twitter by this link - https://api.twitter.com/1.1/account/verify_credentials.json

To do this first I request for oauth_token, in second step I called for signature and in last I tried to get user data using oauth_token and signature. In this last call I am getting "401 unauthorized error".

I have used this cordova plugin - http://ngcordova.com/docs/plugins/oauth/

I want to generate APK for android and ios.I am using cordova CLI to generate apk.

Here is my code -

class TwitterLoginCtrl extends BaseCtrl
  @register 'TwitterLoginCtrl'

  @inject '$scope', '$ionicPopup', 'WtaLoading', '$ionicLoading','$cordovaOauth','$http','$cordovaOauthUtility'

  initialize: ->
    @$scope.twitterSignIn = @twitterSignIn

  twitterSignIn: =>
    alert 'Twitter Nisarg'
    @$cordovaOauth.twitter('[consumer key]','consumer secret').then ((result) =>
      console.log JSON.stringify(result)

      oauthObject =
        oauth_consumer_key: '[consumer key]'
        oauth_nonce: @$cordovaOauthUtility.createNonce(10)
        oauth_signature_method: 'HMAC-SHA1'
        oauth_timestamp: Math.round((new Date).getTime() / 1000.0)
        oauth_version: '1.0'
        oauth_token: result.oauth_token
      signatureObj = @$cordovaOauthUtility.createSignature('GET', 'https://api.twitter.com/1.1/account/verify_credentials.json', oauthObject, oauthObject)

      @$http(url: 'https://api.twitter.com/1.1/account/verify_credentials.json', method: 'GET' , params: angular.extend(oauthObject, oauth_signature: signatureObj.signature), format: "json"). then (data) => **// THIS CALL THROWS 401 UNAUTHORIZED ERROR**
        console.log JSON.stringify(data)
    ), (error) ->
      console.log error

EDIT

@$cordovaOauth.twitter('[consumer key]','consumer secret').then ((result) =>

Above call give me this result =>

{
 "oauth_token":[access-token], 
 "oauth_token_secret":[access_token_secret], 
 "user_id":"4629415817", 
 "screen_name":"napster", 
 "x_auth_expires":"0"
}

Here every time I am getting x_auth_expires. Is it ok?

Nisarg
  • 3,024
  • 5
  • 32
  • 54
  • Yes. I have answers. Is this your first App? What is your target platform? Are you using [CLI, SDK or Build](https://github.com/jessemonroy650/top-phonegap-mistakes/blob/master/new-to-Phonegap.md#001)? Please answer these questions in your post. And reply here so I know you have responded. –  Dec 29 '15 at 01:00
  • My target platform is android and ios. To debugging right now I build app then install in mobile and checking logs by eclipse. My PC is slow so I am not using emulator. – Nisarg Dec 29 '15 at 03:55
  • *Please all the answers put in your post.* Please answer the question: **Is this your first App?** –  Dec 29 '15 at 06:18
  • Yes. This is my first app – Nisarg Dec 29 '15 at 09:55
  • what you get is the answer of successful login. See e.g. this link: https://github.com/fightbulc/simplon_twitter how to proceed to use api with the access token. xauthexpires is zero, meaning not YET expired == ok! – mico Jan 10 '16 at 18:05
  • @mico : Ok. Why I am getting 401 unauthorized error in `@$http(url: 'https://api.twitter.com/1.1/account/verify_credentials.json', method: 'GET' , params: angular.extend(oauthObject, oauth_signature: signatureObj.signature), format: "json")` call? – Nisarg Jan 11 '16 at 04:14

1 Answers1

0

As per documentation,

Error Codes & Responses HTTP Status Codes listed saysenter image description here,

When I referred Twitter OAuth : Invalid or expired token [its NOT duplicate], once you get the callback, initialize the class again with new access token.

Community
  • 1
  • 1
byJeevan
  • 3,728
  • 3
  • 37
  • 60