[GH-ISSUE #382] Cannot Authorize Google API #246

Closed
opened 2026-03-03 16:47:01 +03:00 by kerem · 7 comments
Owner

Originally created by @haithngn on GitHub (Jun 13, 2017).
Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/382

Description:

OAuth Provider (Google, ..):

OAuth Version:

  • Version 1
  • Version 2

OS (Please fill the version) :

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

Installation method:

  • Carthage
  • CocoaPods
  • Manually

Library version:

  • head
  • v1.0.0
  • v0.6
  • other: (Please fill in the version you are using.)

Xcode version:

  • 8.3.3 (Swift 3.0)

  • 8.0 (Swift 2.3)

  • 7.3.1

  • other: (Please fill in the version you are using.)

  • Swift

I'm trying to authorize Google APIs but after redirect to the API I always receive the error

The operation couldn’t be completed. (OAuthSwiftError error -11.) Optional(Error Domain=NSURLErrorDomain Code=401 "" UserInfo={Response-Body={
"error" : "invalid_client"
}, NSErrorFailingURLKey=https://accounts.google.com/o/oauth2/token, Response-Headers={
"Cache-Control" = "no-cache, no-store, max-age=0, must-revalidate";
"Content-Encoding" = gzip;
"Content-Type" = "application/json; charset=utf-8";
Date = "Tue, 13 Jun 2017 10:41:10 GMT";
Expires = "Mon, 01 Jan 1990 00:00:00 GMT";
Pragma = "no-cache";
Server = ESF;
"alt-svc" = "quic=":443"; ma=2592000; v="38,37,36,35"";
"x-frame-options" = SAMEORIGIN;
"x-xss-protection" = "1; mode=block";
}, OAuthSwiftError.response=<NSHTTPURLResponse: 0x608000429b80> { URL: https://accounts.google.com/o/oauth2/token } { status code: 401, headers {
"Cache-Control" = "no-cache, no-store, max-age=0, must-revalidate";
"Content-Encoding" = gzip;
"Content-Type" = "application/json; charset=utf-8";
Date = "Tue, 13 Jun 2017 10:41:10 GMT";
Expires = "Mon, 01 Jan 1990 00:00:00 GMT";
Pragma = "no-cache";
Server = ESF;
"alt-svc" = "quic=":443"; ma=2592000; v="38,37,36,35"";
"x-frame-options" = SAMEORIGIN;
"x-xss-protection" = "1; mode=block";
} }, OAuthSwiftError.response.data=<7b0a2020 22657272 6f722220 3a202269 6e76616c 69645f63 6c69656e 74220a7d>, NSLocalizedDescription=})

Here are my codes:

oauthswift = OAuth2Swift(
consumerKey: params.clientID,
consumerSecret: params.clientSecret,
authorizeUrl: "https://accounts.google.com/o/oauth2/v2/auth",
accessTokenUrl: "https://accounts.google.com/o/oauth2/token",
responseType: "code"
)

    func generateState(withLength len: Int) -> String {
        let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
        let length = UInt32(letters.characters.count)
        
        var randomString = ""
        for _ in 0..<len {
            let rand = arc4random_uniform(length)
            let idx = letters.index(letters.startIndex, offsetBy: Int(rand))
            let letter = letters.characters[idx]
            randomString += String(letter)
        }
        return randomString
    }
    
    let state = generateState(withLength: 20)
    let _ = oauthswift.authorize(
        withCallbackURL: URL(string: "com.googleusercontent.apps.11348411501-clientidxxx:/oauth")!, scope: "https://mail.google.com", state: state,
        success: { credential, response, parameters in
            print(credential)
            print(response)
            print(parameters)
    },
        failure: { error in
            print("error \(error.localizedDescription) \((error as OAuthSwiftError).underlyingError)")
    }
    )
Originally created by @haithngn on GitHub (Jun 13, 2017). Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/382 ### Description: ### OAuth Provider (Google, ..): ### 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.0.0 - [ ] v0.6 - [ ] other: (Please fill in the version you are using.) ### Xcode version: - [x] 8.3.3 (Swift 3.0) - [ ] 8.0 (Swift 2.3) - [ ] 7.3.1 - [ ] other: (Please fill in the version you are using.) - [x] Swift I'm trying to authorize Google APIs but after redirect to the API I always receive the error > The operation couldn’t be completed. (OAuthSwiftError error -11.) Optional(Error Domain=NSURLErrorDomain Code=401 "" UserInfo={Response-Body={ > "error" : "invalid_client" > }, NSErrorFailingURLKey=https://accounts.google.com/o/oauth2/token, Response-Headers={ > "Cache-Control" = "no-cache, no-store, max-age=0, must-revalidate"; > "Content-Encoding" = gzip; > "Content-Type" = "application/json; charset=utf-8"; > Date = "Tue, 13 Jun 2017 10:41:10 GMT"; > Expires = "Mon, 01 Jan 1990 00:00:00 GMT"; > Pragma = "no-cache"; > Server = ESF; > "alt-svc" = "quic=\":443\"; ma=2592000; v=\"38,37,36,35\""; > "x-frame-options" = SAMEORIGIN; > "x-xss-protection" = "1; mode=block"; > }, OAuthSwiftError.response=<NSHTTPURLResponse: 0x608000429b80> { URL: https://accounts.google.com/o/oauth2/token } { status code: 401, headers { > "Cache-Control" = "no-cache, no-store, max-age=0, must-revalidate"; > "Content-Encoding" = gzip; > "Content-Type" = "application/json; charset=utf-8"; > Date = "Tue, 13 Jun 2017 10:41:10 GMT"; > Expires = "Mon, 01 Jan 1990 00:00:00 GMT"; > Pragma = "no-cache"; > Server = ESF; > "alt-svc" = "quic=\":443\"; ma=2592000; v=\"38,37,36,35\""; > "x-frame-options" = SAMEORIGIN; > "x-xss-protection" = "1; mode=block"; > } }, OAuthSwiftError.response.data=<7b0a2020 22657272 6f722220 3a202269 6e76616c 69645f63 6c69656e 74220a7d>, NSLocalizedDescription=}) Here are my codes: > oauthswift = OAuth2Swift( > consumerKey: params.clientID, > consumerSecret: params.clientSecret, > authorizeUrl: "https://accounts.google.com/o/oauth2/v2/auth", > accessTokenUrl: "https://accounts.google.com/o/oauth2/token", > responseType: "code" > ) > > func generateState(withLength len: Int) -> String { > let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" > let length = UInt32(letters.characters.count) > > var randomString = "" > for _ in 0..<len { > let rand = arc4random_uniform(length) > let idx = letters.index(letters.startIndex, offsetBy: Int(rand)) > let letter = letters.characters[idx] > randomString += String(letter) > } > return randomString > } > > let state = generateState(withLength: 20) > let _ = oauthswift.authorize( > withCallbackURL: URL(string: "com.googleusercontent.apps.11348411501-clientidxxx:/oauth")!, scope: "https://mail.google.com", state: state, > success: { credential, response, parameters in > print(credential) > print(response) > print(parameters) > }, > failure: { error in > print("error \(error.localizedDescription) \((error as OAuthSwiftError).underlyingError)") > } > )
kerem closed this issue 2026-03-03 16:47:01 +03:00
Author
Owner

@haithngn commented on GitHub (Jun 15, 2017):

I have a another error
https://stackoverflow.com/questions/44556797/google-oauth-2-0-invalid-request-missing-scheme

Could you please help ?

oauthswift = OAuth2Swift(
consumerKey: "984813079630-f828a92sqtl5lgumd4kgp9i30bs9og09.apps.googleusercontent.com",
consumerSecret: "AIzaSyD13MBv78yWIjl4TX9jOOT9AWuEkYdVSPQ",
authorizeUrl: "https://accounts.google.com/o/oauth2/v2/auth",
accessTokenUrl: "https://www.googleapis.com/oauth2/v4/token",
responseType: "code"
)
let handle = oauthswift.authorize(
withCallbackURL: URL(string: "com.googleusercontent.apps.984813079630-f828a92sqtl5lgumd4kgp9i30bs9og09")!,
scope: "profile", state:"GOOGLE",
success: { credential, response, parameters in
print(credential.oauthToken)
// Do your request
},
failure: { error in
print(error.localizedDescription)
}
)

<!-- gh-comment-id:308617606 --> @haithngn commented on GitHub (Jun 15, 2017): I have a another error https://stackoverflow.com/questions/44556797/google-oauth-2-0-invalid-request-missing-scheme Could you please help ? > oauthswift = OAuth2Swift( > consumerKey: "984813079630-f828a92sqtl5lgumd4kgp9i30bs9og09.apps.googleusercontent.com", > consumerSecret: "AIzaSyD13MBv78yWIjl4TX9jOOT9AWuEkYdVSPQ", > authorizeUrl: "https://accounts.google.com/o/oauth2/v2/auth", > accessTokenUrl: "https://www.googleapis.com/oauth2/v4/token", > responseType: "code" > ) > let handle = oauthswift.authorize( > withCallbackURL: URL(string: "com.googleusercontent.apps.984813079630-f828a92sqtl5lgumd4kgp9i30bs9og09")!, > scope: "profile", state:"GOOGLE", > success: { credential, response, parameters in > print(credential.oauthToken) > // Do your request > }, > failure: { error in > print(error.localizedDescription) > } > )
Author
Owner

@phimage commented on GitHub (Jun 15, 2017):

a scheme is word before :// like http://
your URL has no scheme in URL(string: "com.googleusercontent.apps.984813079630-f828a92sqtl5lgumd4kgp9i30bs9og09")

in many oauth provider you must register a callback url yourself in google app that you define
and this is the only authorized
then see readme, callback url it's useful to get retrieve in your app the credential/token

<!-- gh-comment-id:308654432 --> @phimage commented on GitHub (Jun 15, 2017): a scheme is word before :// like http:// your URL has no scheme in `URL(string: "com.googleusercontent.apps.984813079630-f828a92sqtl5lgumd4kgp9i30bs9og09")` in many oauth provider you must register a callback url yourself in google app that you define and this is the only authorized then see readme, callback url it's useful to get retrieve in your app the credential/token
Author
Owner

@haithngn commented on GitHub (Jun 15, 2017):

@phimage ,
I have changed the scheme to "ios_app_bundle_id:/oauthRedirectCallback" then this problem still there

{
 "error": "invalid_client",
 "error_description": "Unauthorized"
}

I also tried use this Framework https://github.com/openid/AppAuth-iOS with same google app config and it works fine.

<!-- gh-comment-id:308762878 --> @haithngn commented on GitHub (Jun 15, 2017): @phimage , I have changed the scheme to "ios_app_bundle_id:/oauthRedirectCallback" then this problem still there ``` { "error": "invalid_client", "error_description": "Unauthorized" } ``` I also tried use this Framework https://github.com/openid/AppAuth-iOS with same google app config and it works fine.
Author
Owner

@phimage commented on GitHub (Jun 15, 2017):

I make a test with callback https://oauthswift.herokuapp.com/callback/google/
http one which redirect to oauthswift://
I declare in google api console
You url is not declarable in google api console , google need http scheme
https://github.com/OAuthSwift/OAuthSwift/wiki/API-with-only-HTTP-scheme-into-callback-URL

I just add oauthswift.allowMissingStateCheck = true
and that works fine

<!-- gh-comment-id:308779538 --> @phimage commented on GitHub (Jun 15, 2017): I make a test with callback https://oauthswift.herokuapp.com/callback/google/ http one which redirect to oauthswift:// I declare in google api console You url is not declarable in google api console , google need http scheme https://github.com/OAuthSwift/OAuthSwift/wiki/API-with-only-HTTP-scheme-into-callback-URL I just add `oauthswift.allowMissingStateCheck = true` and that works fine
Author
Owner

@haithngn commented on GitHub (Jun 15, 2017):

I will try oauthswift.allowMissingStateCheck = true

actually, we cannot declare a custom callback url for ios client id
screen shot 2017-06-15 at 10 52 30 pm

<!-- gh-comment-id:308780817 --> @haithngn commented on GitHub (Jun 15, 2017): I will try oauthswift.allowMissingStateCheck = true actually, we cannot declare a custom callback url for ios client id ![screen shot 2017-06-15 at 10 52 30 pm](https://user-images.githubusercontent.com/22902671/27190026-60273b26-521d-11e7-8552-d48e516aa884.png)
Author
Owner

@phimage commented on GitHub (Jun 15, 2017):

I create a web client app, not an ios one

<!-- gh-comment-id:308793062 --> @phimage commented on GitHub (Jun 15, 2017): I create a web client app, not an ios one
Author
Owner

@haithngn commented on GitHub (Jun 15, 2017):

ok, I will investigate more

<!-- gh-comment-id:308796455 --> @haithngn commented on GitHub (Jun 15, 2017): ok, I will investigate more
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#246
No description provided.