[GH-ISSUE #479] Creating Authorization header for custom requests #315

Closed
opened 2026-03-03 16:47:37 +03:00 by kerem · 4 comments
Owner

Originally created by @legis18 on GitHub (Jul 12, 2018).
Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/479

Description:

I'm trying to create my own request with URLSessionDataTask with Authorization header created using method credential.makeHeaders but I always get response 401 from server. Is it something wrong with this header?

OAuth Provider? (Twitter, Github, ..):

Twitter

OAuth Version:

  • Version 1
  • Version 2

OS (Please fill the version) :

  • iOS :
  • OSX :
  • TVOS :
  • WatchOS :

Installation method:

  • Carthage
  • CocoaPods
  • Manually

Library version:

  • head
  • v1.2.1
  • v1.2 (Swift 4.0)
  • v1.0.0
  • v0.6
  • other: (1.2.2.)

Xcode version:

  • 9.3 (Swift 4.1)

  • 9.0 (Swift 4.0)

  • 9.0 (Swift 3.2)

  • 8.x (Swift 3.x)

  • 8.0 (Swift 2.3)

  • 7.3.1

  • other: (9.4 Swift)

  • objective c

Originally created by @legis18 on GitHub (Jul 12, 2018). Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/479 ### Description: I'm trying to create my own request with URLSessionDataTask with Authorization header created using method `credential.makeHeaders` but I always get response 401 from server. Is it something wrong with this header? ### OAuth Provider? (Twitter, Github, ..): Twitter ### OAuth Version: - [x] Version 1 - [ ] Version 2 ### OS (Please fill the version) : - [x] iOS : - [ ] OSX : - [ ] TVOS : - [ ] WatchOS : ### Installation method: - [ ] Carthage - [x] CocoaPods - [ ] Manually ### Library version: - [ ] head - [ ] v1.2.1 - [ ] v1.2 (Swift 4.0) - [ ] v1.0.0 - [ ] v0.6 - [x] other: (1.2.2.) ### Xcode version: - [ ] 9.3 (Swift 4.1) - [ ] 9.0 (Swift 4.0) - [ ] 9.0 (Swift 3.2) - [ ] 8.x (Swift 3.x) - [ ] 8.0 (Swift 2.3) - [ ] 7.3.1 - [x] other: (9.4 Swift) - [ ] objective c
kerem closed this issue 2026-03-03 16:47:37 +03:00
Author
Owner

@phimage commented on GitHub (Jul 12, 2018):

You do not provide enough information about your code
ie. value passed to credential.makeHeaders
and task executed with this header

Good to know, with oauth 1 you must compute a header for each request to sign it according to the url and query parameters
Then the doc

    /// for OAuth1 parameters must contains sorted query parameters and url must not contains query parameters

to achieve that in OauthSwift a lot of work is done in updateRequest function in OAuthSwiftHTTPRequest

<!-- gh-comment-id:404490500 --> @phimage commented on GitHub (Jul 12, 2018): You do not provide enough information about your code ie. value passed to credential.makeHeaders and task executed with this header Good to know, with oauth 1 you must compute a header for each request to sign it according to the url and query parameters Then the doc ``` /// for OAuth1 parameters must contains sorted query parameters and url must not contains query parameters ``` to achieve that in OauthSwift a lot of work is done in updateRequest function in OAuthSwiftHTTPRequest
Author
Owner

@legis18 commented on GitHub (Jul 12, 2018):

Thanks for response

So my code is something like this:
`
let htURL = URL(string: "https://api.twitter.com/1.1/statuses/home_timeline.json")!

    auth.client.credential.oauthToken = myoauthtoken
    auth.client.credential.oauthTokenSecret = myoauthtokensecret
    let params = auth.parameters
    
    let headers = auth.client.credential.makeHeaders(htURL, method: .GET, parameters: params)
    let header: String = headers["Authorization"]!
    
    var request = URLRequest(url: htURL)
    request.httpMethod = "GET"
    request.addValue(header , forHTTPHeaderField: "Authorization")
    let task = URLSession.shared.dataTask(with: request) {...}`
<!-- gh-comment-id:404501896 --> @legis18 commented on GitHub (Jul 12, 2018): Thanks for response So my code is something like this: ` let htURL = URL(string: "https://api.twitter.com/1.1/statuses/home_timeline.json")! auth.client.credential.oauthToken = myoauthtoken auth.client.credential.oauthTokenSecret = myoauthtokensecret let params = auth.parameters let headers = auth.client.credential.makeHeaders(htURL, method: .GET, parameters: params) let header: String = headers["Authorization"]! var request = URLRequest(url: htURL) request.httpMethod = "GET" request.addValue(header , forHTTPHeaderField: "Authorization") let task = URLSession.shared.dataTask(with: request) {...}`
Author
Owner

@phimage commented on GitHub (Jul 12, 2018):

did you try with an empty params
even if I do not think you can achieve with that
Then I think that when doing the request there is a log of parameters to add starting with oauth_

your url must contains all query parameters

<!-- gh-comment-id:404506888 --> @phimage commented on GitHub (Jul 12, 2018): did you try with an empty `params` even if I do not think you can achieve with that Then I think that when doing the request there is a log of parameters to add starting with oauth_ your url must contains all query parameters
Author
Owner

@legis18 commented on GitHub (Jul 12, 2018):

wow, that worked! I waste so many time understanding what I'm doing wrong. Thanks for your help!

<!-- gh-comment-id:404511920 --> @legis18 commented on GitHub (Jul 12, 2018): wow, that worked! I waste so many time understanding what I'm doing wrong. Thanks for your help!
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/OAuthSwift#315
No description provided.