【问题标题】:how to pass token and token seceret to oauth swift如何将令牌和令牌秘密传递给oauth swift
【发布时间】:2018-01-16 06:36:30
【问题描述】:

如何在 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 库中传递它。

我尝试了以下代码

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

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

【问题讨论】:

    标签: ios oauth swift4 oauth-1.0a


    【解决方案1】:

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

    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 token和secret可以吗?
    • 此代码用于 OAuth 实现,如果它不是您的要求,则可以跳过它,但 API 受 OAuth 1.x 保护,那么您必须将此凭据传递给客户端
    猜你喜欢
    • 2013-06-13
    • 2012-04-28
    • 1970-01-01
    • 2013-03-16
    • 1970-01-01
    • 2011-03-18
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    相关资源
    最近更新 更多