如何将令牌和令牌机密传递给 oauth swift
how to pass token and token seceret to oauth swift
如何在 oauth swift lib
上传递令牌和令牌机密
我在 git 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
//...
}
)
但是除了 consumerKey 和 consumerSecret,我还有 token 和 TokenSecret。所以我可以在 oAuthSwift lib 中传递它。
我尝试了以下代码
let oauthswift = OAuth1Swift(
consumerKey:"102xxxxxxxxxxxxxxxxxxxxxxxxxxxx",
consumerSecret:"5xxxxxxxxxxxxxxxxxxxxxxxxxxx",
token:"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
tokenSeceret:"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
)
但它给出了 "Cannot invoke initializer for type 'OAuth1Swift' with an argument list" 的错误
此库中还有另一个可用的初始化程序,如下所示
// 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)
}
)
但是我们没有这样的链接来获取令牌,而是我们使用已经创建的令牌和秘密
在调用请求之前使用以下两行
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
//...
}
)
如何在 oauth swift lib
上传递令牌和令牌机密我在 git 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
//...
}
)
但是除了 consumerKey 和 consumerSecret,我还有 token 和 TokenSecret。所以我可以在 oAuthSwift lib 中传递它。
我尝试了以下代码
let oauthswift = OAuth1Swift(
consumerKey:"102xxxxxxxxxxxxxxxxxxxxxxxxxxxx",
consumerSecret:"5xxxxxxxxxxxxxxxxxxxxxxxxxxx",
token:"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
tokenSeceret:"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
)
但它给出了 "Cannot invoke initializer for type 'OAuth1Swift' with an argument list" 的错误 此库中还有另一个可用的初始化程序,如下所示
// 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)
}
)
但是我们没有这样的链接来获取令牌,而是我们使用已经创建的令牌和秘密
在调用请求之前使用以下两行
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
//...
}
)