如何将令牌和令牌seceret传递给oauth swift

时间:2018-01-16 06:36:31

标签: ios oauth swift4 oauth-1.0a

如何在oauth swift lib上传递令牌和令牌密钥

我在lib

的git文档中找到了以下代码
// 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
        //...
    }
)

但是与consumerKey和consumerSecret一起,我也有令牌和TokenSecret。所以我可以在oAuthSwift lib中传递它。

我尝试了以下代码

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

但它给出的错误是"无法为类型' OAuth1Swift'调用初始值设定项。用参数列表" 这个库中有另一个初始化程序,如下所示

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

但是我们没有这样的链接来获取令牌,而是我们使用已经创建的令牌和秘密

1 个答案:

答案 0 :(得分:0)

在调用请求之前使用以下两行

var arr = [1, 2, 4, 3, 5, 6, 7, 8, 9];
var length = arr.length;
for (var i = 0; i < length; i++) {
  console.log(i,arr[i]);
  if (arr[i] % 2 == 0) {
    arr.push(arr.splice(i, 1)[0]);
    console.log("result ", arr)
    length--;
    i--;
  }
}
console.log(arr)

即您的代码变为

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