0

How to pass token and token secret on oauth swift lib

i found following code on git documentation of lib

// create an instance and retain it
let oauthswift = OAuth1Swift(
    consumerKey:    "********",
    consumerSecret: "********"
)
// do your HTTP request without authorize
oauthswift.client.get("https://api.example.com/foo/bar",
    success: { response in
        //....
    },
    failure: { error in
        //...
    }
)

But along with consumerKey and consumerSecret i have token and TokenSecret too. so where i can pass that in oAuthSwift lib.

I tried following code

let  oauthswift = OAuth1Swift(
            consumerKey:"102xxxxxxxxxxxxxxxxxxxxxxxxxxxx",
            consumerSecret:"5xxxxxxxxxxxxxxxxxxxxxxxxxxx",
            token:"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
            tokenSeceret:"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
        )

but it gives error that "Cannot invoke initializer for type 'OAuth1Swift' with an argument list" there is Another initializer available in this lib as follow

// create an instance and retain it
oauthswift = OAuth1Swift(
    consumerKey:    "********",
    consumerSecret: "********",
    requestTokenUrl: "https://api.twitter.com/oauth/request_token",
    authorizeUrl:    "https://api.twitter.com/oauth/authorize",
    accessTokenUrl:  "https://api.twitter.com/oauth/access_token"
)
// authorize
let handle = oauthswift.authorize(
    withCallbackURL: URL(string: "oauth-swift://oauth-callback/twitter")!,
    success: { credential, response, parameters in
      print(credential.oauthToken)
      print(credential.oauthTokenSecret)
      print(parameters["user_id"])
      // Do your request
    },
    failure: { error in
      print(error.localizedDescription)
    }             
)

but we don't have such links to get token instead we used already created token and secrete

1 Answers1

4

Use Following Two lines before calling request

oauthswift.client.credential.oauth_token = {your stored token}
oauthswift.client.credential.oauth_token_secret = {your stored secret token}

i.e Your code become

// create an instance and retain it
let oauthswift = OAuth1Swift(
    consumerKey:    "********",
    consumerSecret: "********"
)

//Set Token and TokenSecret
    oauthswift.client.credential.oauth_token = "asxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    oauthswift.client.credential.oauth_token_secret = "1cxxxxxxxxxxxxxxxxxxxxxxx"


// do your HTTP request
oauthswift.client.get("https://api.example.com/foo/bar",
    success: { response in
        //....
    },
    failure: { error in
        //...
    }
)
Vinayak Khedkar
  • 504
  • 5
  • 13