[GH-ISSUE #512] How to extract HTTP status code from OAuthSwiftError? #334

Closed
opened 2026-03-03 16:47:48 +03:00 by kerem · 1 comment
Owner

Originally created by @abinrm on GitHub (Feb 7, 2019).
Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/512

Description:

How to extract HTTP status code from OAuthSwiftError?
When wrong credentials is given for login it returns 401 in Postman app
postman 2019-02-07 18-24-11 1

When I enter wrong credentials in my app as below, it goes to failure block and all I can get is an OAuthSwiftError.

oAuth2 = OAuth2Swift(
            consumerKey: "*****",
            consumerSecret: "*****",
            authorizeUrl: "*****/oauth/authorize",
            accessTokenUrl: "*****/oauth/token",
            responseType: "token"
        )

        oAuth2?.authorize(username: "wrong username",
                          password: "wrong password",
                          scope: nil,
                          success: { credentials, response, parameters in
                                   // sucess block
                            },
                          failure: {  error in
                            print(error)
                            })

I would like to display an alert on the wrong credentials. I prefer displaying Unauthorised / username and password doesn't match over alert saying some error occurred
The error I get is as below, So how can I extract the HTTP status code from this error?

requestError[Error Domain=NSURLErrorDomain Code=401 "invalid_grant The provided authorization grant is invalid, expired, revoked, does not match the redirection URI used in the authorization request, or was issued to another client." UserInfo={Response-Body={"error":"invalid_grant","error_description":"The provided authorization grant is invalid, expired, revoked, does not match the redirection URI used in the authorization request, or was issued to another client."}, NSErrorFailingURLKey=https://BASEURL/oauth/token, NSLocalizedDescription=invalid_grant The provided authorization grant is invalid, expired, revoked, does not match the redirection URI used in the authorization request, or was issued to another client., OAuthSwiftError.response.data=<7b226572 726f7222 3a22696e 76616c69 645f6772 616e7422 2c226572 726f725f 64657363 72697074 696f6e22 3a225468 65207072 6f766964 65642061 7574686f 72697a61 74696f6e 20677261 6e742069 7320696e 76616c69 642c2065 78706972 65642c20 7265766f 6b65642c 20646f65 73206e6f 74206d61 74636820 74686520 72656469 72656374 696f6e20 55524920 75736564 20696e20 74686520 61757468 6f72697a 6174696f 6e207265 71756573 742c206f 72207761 73206973 73756564 20746f20 616e6f74 68657220 636c6965 6e742e22 7d>, Response-Headers={
    "Cache-Control" = "private, no-store";
    Connection = "keep-alive";
    "Content-Type" = "application/json; charset=utf-8";
    Date = "Thu, 07 Feb 2019 17:41:33 GMT";
    Pragma = "no-cache";
    Server = Cowboy;
    "Strict-Transport-Security" = "max-age=31536000; includeSubDomains";
    "Transfer-Encoding" = Identity;
    Via = "1.1 vegur";
    "Www-Authenticate" = "Bearer realm=\"Doorkeeper\", error=\"invalid_grant\", error_description=\"The provided authorization grant is invalid, expired, revoked, does not match the redirection URI used in the authorization request, or was issued to another client.\"";
    "X-Request-Id" = "a2be7354-6bbd-4140-9b23-7ef488041d24";
    "X-Runtime" = "0.122216";
}, OAuthSwiftError.response=<NSHTTPURLResponse: 0x282abd6c0> { URL: https://BASEURL/oauth/token } { Status Code: 401, Headers {
    "Cache-Control" =     (
        "private, no-store"
    );
    Connection =     (
        "keep-alive"
    );
    "Content-Type" =     (
        "application/json; charset=utf-8"
    );
    Date =     (
        "Thu, 07 Feb 2019 17:41:33 GMT"
    );
    Pragma =     (
        "no-cache"
    );
    Server =     (
        Cowboy
    );
    "Strict-Transport-Security" =     (
        "max-age=31536000; includeSubDomains"
    );
    "Transfer-Encoding" =     (
        Identity
    );
    Via =     (
        "1.1 vegur"
    );
    "Www-Authenticate" =     (
        "Bearer realm=\"Doorkeeper\", error=\"invalid_grant\", error_description=\"The provided authorization grant is invalid, expired, revoked, does not match the redirection URI used in the authorization request, or was issued to another client.\""
    );
    "X-Request-Id" =     (
        "a2be7354-6bbd-4140-9b23-7ef488041d24"
    );
    "X-Runtime" =     (
        "0.122216"
    );
} }}]

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

Third Party

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: (Please fill in the version you are using.)

Xcode version:

  • 10.1 (Swift 4.2)

  • 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: (Please fill in the version you are using.)

  • objective c

Originally created by @abinrm on GitHub (Feb 7, 2019). Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/512 ### Description: How to extract HTTP status code from OAuthSwiftError? When wrong credentials is given for login it returns 401 in Postman app ![postman 2019-02-07 18-24-11 1](https://user-images.githubusercontent.com/36157771/52430664-e8614e00-2b06-11e9-897c-462034e9de56.png) When I enter wrong credentials in my app as below, it goes to failure block and all I can get is an `OAuthSwiftError`. ``` oAuth2 = OAuth2Swift( consumerKey: "*****", consumerSecret: "*****", authorizeUrl: "*****/oauth/authorize", accessTokenUrl: "*****/oauth/token", responseType: "token" ) oAuth2?.authorize(username: "wrong username", password: "wrong password", scope: nil, success: { credentials, response, parameters in // sucess block }, failure: { error in print(error) }) ``` I would like to display an alert on the wrong credentials. I prefer displaying **Unauthorised / username and password doesn't match** over alert saying `some error occurred` The error I get is as below, So how can I extract the HTTP status code from this error? ``` requestError[Error Domain=NSURLErrorDomain Code=401 "invalid_grant The provided authorization grant is invalid, expired, revoked, does not match the redirection URI used in the authorization request, or was issued to another client." UserInfo={Response-Body={"error":"invalid_grant","error_description":"The provided authorization grant is invalid, expired, revoked, does not match the redirection URI used in the authorization request, or was issued to another client."}, NSErrorFailingURLKey=https://BASEURL/oauth/token, NSLocalizedDescription=invalid_grant The provided authorization grant is invalid, expired, revoked, does not match the redirection URI used in the authorization request, or was issued to another client., OAuthSwiftError.response.data=<7b226572 726f7222 3a22696e 76616c69 645f6772 616e7422 2c226572 726f725f 64657363 72697074 696f6e22 3a225468 65207072 6f766964 65642061 7574686f 72697a61 74696f6e 20677261 6e742069 7320696e 76616c69 642c2065 78706972 65642c20 7265766f 6b65642c 20646f65 73206e6f 74206d61 74636820 74686520 72656469 72656374 696f6e20 55524920 75736564 20696e20 74686520 61757468 6f72697a 6174696f 6e207265 71756573 742c206f 72207761 73206973 73756564 20746f20 616e6f74 68657220 636c6965 6e742e22 7d>, Response-Headers={ "Cache-Control" = "private, no-store"; Connection = "keep-alive"; "Content-Type" = "application/json; charset=utf-8"; Date = "Thu, 07 Feb 2019 17:41:33 GMT"; Pragma = "no-cache"; Server = Cowboy; "Strict-Transport-Security" = "max-age=31536000; includeSubDomains"; "Transfer-Encoding" = Identity; Via = "1.1 vegur"; "Www-Authenticate" = "Bearer realm=\"Doorkeeper\", error=\"invalid_grant\", error_description=\"The provided authorization grant is invalid, expired, revoked, does not match the redirection URI used in the authorization request, or was issued to another client.\""; "X-Request-Id" = "a2be7354-6bbd-4140-9b23-7ef488041d24"; "X-Runtime" = "0.122216"; }, OAuthSwiftError.response=<NSHTTPURLResponse: 0x282abd6c0> { URL: https://BASEURL/oauth/token } { Status Code: 401, Headers { "Cache-Control" = ( "private, no-store" ); Connection = ( "keep-alive" ); "Content-Type" = ( "application/json; charset=utf-8" ); Date = ( "Thu, 07 Feb 2019 17:41:33 GMT" ); Pragma = ( "no-cache" ); Server = ( Cowboy ); "Strict-Transport-Security" = ( "max-age=31536000; includeSubDomains" ); "Transfer-Encoding" = ( Identity ); Via = ( "1.1 vegur" ); "Www-Authenticate" = ( "Bearer realm=\"Doorkeeper\", error=\"invalid_grant\", error_description=\"The provided authorization grant is invalid, expired, revoked, does not match the redirection URI used in the authorization request, or was issued to another client.\"" ); "X-Request-Id" = ( "a2be7354-6bbd-4140-9b23-7ef488041d24" ); "X-Runtime" = ( "0.122216" ); } }}] ``` ### OAuth Provider? (Twitter, Github, ..): Third Party ### OAuth Version: - [ ] Version 1 - [x] Version 2 ### OS (Please fill the version) : - [x] iOS : - [ ] OSX : - [ ] TVOS : - [ ] WatchOS : ### Installation method: - [x] Carthage - [ ] CocoaPods - [ ] Manually ### Library version: - [x] head - [ ] v1.2.1 - [ ] v1.2 (Swift 4.0) - [ ] v1.0.0 - [ ] v0.6 - [ ] other: (Please fill in the version you are using.) ### Xcode version: - [x] 10.1 (Swift 4.2) - [ ] 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: (Please fill in the version you are using.) - [ ] objective c
kerem 2026-03-03 16:47:48 +03:00
  • closed this issue
  • added the
    question
    label
Author
Owner

@phimage commented on GitHub (Feb 21, 2019):

if you recieve an requestError
get the error with .error or with switch case
then get code in error: .error?.code

see how I construct the error from response (URLResponse)
.requestError(error: NSError(domain: NSURLErrorDomain, code: response.statusCode, userInfo: userInfo), request: request))

<!-- gh-comment-id:465982477 --> @phimage commented on GitHub (Feb 21, 2019): if you recieve an requestError get the error with .error or with switch case then get code in error: .error?.code see how I construct the error from response (URLResponse) `.requestError(error: NSError(domain: NSURLErrorDomain, code: response.statusCode, userInfo: userInfo), request: request))`
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#334
No description provided.