寻找一种快速生成 OAuth 1 签名的方法 [英] Looking for a swift way to generate OAuth 1 signature
问题描述
我正在开发一个小型 swift 程序,通过 OAuth-1 使用 Yelp API.我希望生成 HMAC-SHA1 签名.
I'm working on a small swift program to work with Yelp API over OAuth-1. I am looking to generate a HMAC-SHA1 signature.
我有客户密钥、秘密密钥、令牌和令牌秘密.
I have the customer key, secret key , token and token secret.
据我所知,要使用 OAuth 1 发出 API 请求,我们需要以下属性:
From what I know, to make an API request with OAuth 1, we need the following attributes :
- oauth_consumer_key
- oauth_token
- oauth_signature_method = (HMAC-SHA1)
- oauth_signature
- oauth_timestamp
- oauth_nonce
我如何生成#4,5,6
How do I generate #4,5,6
我看了其他这个,但没有帮助.
I looked other this, but didn't help.
TIA!
推荐答案
响应很晚,但我维护了一个非常轻量级的 Swift 3 易于使用的扩展,它为 URLRequest 添加了 OAuth 1.0a 功能.
Quite late response but I maintain a very lightweight Swift 3 easy to use extension that adds OAuth 1.0a capabilities to URLRequest.
它被称为 OhhAuth.可以通过 Cocoa Pods 或 Swift 包管理器轻松安装.
It's called OhhAuth. Can be easy installed with Cocoa Pods or the Swift package manager.
pod 'OhhAuth'
我将添加一个关于如何与 Twitter API 交互的使用示例:
I will add an usage example on how to interact with the Twitter API:
let cc = (key: "<YOUR APP CONSUMER KEY>", secret: "<YOUR APP CONSUMER SECRET>")
let uc = (key: "<YOUR USER KEY>", secret: "<YOUR USER SECRET>")
var req = URLRequest(url: URL(string: "https://api.twitter.com/1.1/statuses/update.json")!)
let paras = ["status": "Hey Twitter! \u{1F6A7} Take a look at this sweet UUID: \(UUID())"]
req.oAuthSign(method: "POST", urlFormParameters: paras, consumerCredentials: cc, userCredentials: uc)
let task = URLSession(configuration: .ephemeral).dataTask(with: req) { (data, response, error) in
if let error = error {
print(error)
}
else if let data = data {
print(String(data: data, encoding: .utf8) ?? "Does not look like a utf8 response :(")
}
}
task.resume()
如果你只对签名感兴趣,可以使用:
If you are only interested in the signature, you can use:
OhhAuth.calculateSignature(url: URL, method: String, parameter: [String: String],
consumerCredentials cc: Credentials, userCredentials uc: Credentials?) -> String
这篇关于寻找一种快速生成 OAuth 1 签名的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!