5

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.

From what I know, to make an API request with OAuth 1, we need the following attributes :

  1. oauth_consumer_key
  2. oauth_token
  3. oauth_signature_method = (HMAC-SHA1)
  4. oauth_signature
  5. oauth_timestamp
  6. oauth_nonce

How do I generate #4,5,6

I looked other this, but didn't help.

TIA!

Community
  • 1
  • 1
sau123
  • 343
  • 4
  • 15

2 Answers2

2

Quite late response but I maintain a very lightweight Swift 3 easy to use extension that adds OAuth 1.0a capabilities to URLRequest.

It's called OhhAuth. Can be easy installed with Cocoa Pods or the Swift package manager.

pod 'OhhAuth'

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
LimeRed
  • 998
  • 10
  • 15
  • I am interested only by the signature, but i need my signature is generated with other parameter : oauth_nonce , oauth_timestamp , it's possible ? – Millet Antoine Nov 29 '18 at 16:25
  • @MilletAntoine `oauth_nonce` and `oauth_timestamp` are part of the standard so my library already does that for you automatically. You can validate this here: https://github.com/mw99/OhhAuth/blob/master/Sources/OhhAuth.swift#L134 or just take a look at the generated signature string, it will include the parameters. – LimeRed Nov 30 '18 at 04:16
0

You can use this library OAuthSwift, and if you are working with RxSwift + Moya this one will fit you perfectly RxSocialConnect-iOS

Roberto Frontado
  • 410
  • 5
  • 15