【问题标题】:Reddit API returns 401 (using OAuthSwift)Reddit API 返回 401(使用 OAuthSwift)
【发布时间】:2020-12-25 09:34:28
【问题描述】:

所以我正在尝试让我的示例 reddit 应用程序与 OAuthSwift 一起使用

我从 reddit 收到 401Unauthorized

oauthswift = OAuth2Swift(
     consumerKey:    "reddit_Client_id",
     consumerSecret: "",
     authorizeUrl:   "https://www.reddit.com/api/v1/authorize",
     accessTokenUrl: "https://www.reddit.com/api/v1/access_token",
     responseType:   "code"
)
    
guard let rwURL = URL(string: "redditTestApp://oauth-callback") else { return }
    
handle = oauthswift!.authorize(
        withCallbackURL: rwURL,
        scope: "identity read", state:"reddit") { result in
        switch result {
        case .success(let (credential, response, parameters)):
          print(credential.oauthToken)
          // Do your request
        case .failure(let error):
            print(error.description)
        }
}

我有点迷失在这里我所缺少的东西。我想我已经传递了所有参数,但不确定还剩下什么是为了得到unauthorized 响应。

【问题讨论】:

    标签: ios swift oauth-2.0 authorization reddit


    【解决方案1】:

    所以把它留在这里,以防将来有人需要帮助。

    问题在于,在检索访问令牌期间,reddit 需要一个带有 http 基本身份验证的授权标头。

    OAuthSwift 库提供了一种简单的方法来执行此操作,您只需添加即可

    oauthswift?.accessTokenBasicAuthentification = true

    你就完成了。

    【讨论】:

      【解决方案2】:

      非常感谢!除了那一行之外,我也因同样的问题而失去理智

      oauth.accessTokenBasicAuthentification = true
      

      在授权之前(withCallbackURL...,您还需要将您的client_id作为用户和空字符串作为密码添加到授权参数中

      oauthswift.authorize(withCallbackURL: redirectURL,
                           scope: "identity read",
                           state: "reddit",
                           parameters: ["user": "your_key", "password": "", "duration": "permanent or temporary", "grant_type": "authorization_code"])
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-19
      • 2012-10-30
      • 2019-11-05
      • 1970-01-01
      • 2020-12-05
      • 2015-04-14
      • 1970-01-01
      • 2016-04-05
      相关资源
      最近更新 更多