【问题标题】:Oauth1 iOS get data from APIOauth1 iOS 从 API 获取数据
【发布时间】:2017-10-04 16:02:27
【问题描述】:

如何使用 Oauth1 从 API 获取数据?我只是这样尝试过,但是没有用。

import UIKit
import OAuthSwift

class TestLogin: UIViewController {

var oauthswift: OAuthSwift?
final let urlString = "https://conversation.8villages.com/1.0/contents/articles"


override func viewDidLoad() {
    super.viewDidLoad()

    self.doOAuth()
}


func doOAuth()
{


    let oauthswift = OAuth1Swift(
        consumerKey:    "******",
        consumerSecret: "******",
        requestTokenUrl: "https://oauth.8villages.com/tokens/request-token",
        authorizeUrl:    "https://accounts.8villages.com/oauth/request-token",
        accessTokenUrl:  "https://accounts.8villages.com/oauth/access-token"
    )


    oauthswift.authorize(
        withCallbackURL: URL(string: "https://8villages.com")!,
        success: { credential, response, parameters in
            print(credential.oauthToken)
            print(credential.oauthTokenSecret)
            print(parameters["userId"])
    },
        failure: { error in
            print(error.localizedDescription)
    }             
    )
}

func getHandleURL () {
    let url = NSURL(string: urlString)
    URLSession.shared.dataTask(with: (url as? URL)!, completionHandler: { (data, response, error) -> Void in

        if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary {

            print(jsonObj!.value(forKey: "data"))

        }

    }).resume()
}

}

那么,我该怎么做,或者我需要一个参考示例使用 Oauth1 从 API 获取数据?我只是不知道如何开始使用 OAuth 构建项目,因为我在 google 中搜索,仅教程 OAuth 用于使用社交媒体登录。

【问题讨论】:

    标签: ios swift oauth


    【解决方案1】:

    为了发送 oAuth 1.0 请求,基本上您需要根据您的服务器实现计算正确的查询字符串和正文参数。

    您需要获取以下查询参数:

    • oauth_consumer_key
    • oauth_nonce
    • oauth_signature_method
    • oauth_timestamp
    • oauth_version

    您可以查看this blog,其中对所有参数以及签名过程进行了非常详细的解释。还有this answer指导你如何在iOS中创建HMAC-SHA1签名

    在此过程结束时,您需要根据您的应用程序和服务器都同意的签名方法创建签名。

    然后一个示例 POST 请求应如下所示:取自 oAuth1 guide

    POST /wp-json/wp/v2/posts
    Host: example.com
    Authorization: OAuth
                   oauth_consumer_key="key"
                   oauth_token="token"
                   oauth_signature_method="HMAC-SHA1"
                   oauth_timestamp="123456789",
                   oauth_nonce="nonce",
                   oauth_signature="..."
    
    {
        "title": "Hello World!"
    }
    

    希望对你有帮助。

    【讨论】:

    • 请发博客网址
    • 对不起。请检查。
    猜你喜欢
    • 2017-09-12
    • 2015-09-10
    • 1970-01-01
    • 1970-01-01
    • 2023-02-22
    • 1970-01-01
    • 2021-07-20
    • 2017-05-31
    • 2018-05-11
    相关资源
    最近更新 更多