0

如何在 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 我还有 token 和 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)
    }             
)

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

4

1 回答 1

2

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

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

即你的代码变成

// 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
        //...
    }
)
于 2018-01-17T09:09:40.717 回答