Google OAuth 2.0 invalid_client Unauthorized

I used this code to authorize Google+ via OAuth 2.0

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”
)

  • How do I produce an iOS Release Build that my client can sign on their end?
  • Mach-O Linker Error on Xcode 4.6
  • Need to add a Fixed Overlay like on mapview in IOS
  • Swift - How Can I use 'shouldChangeTextInRange' with Firebase for real time searching?
  • iPhone : How to detect the end of slider drag?
  • How to insert a override function into a if else statement
  •     let handle = oauthswift.authorize(
                withCallbackURL: URL(string: "com.googleusercontent.apps.984813079630-f828a92sqtl5lgumd4kgp9i30bs9og09:/oauth")!,
                scope: "profile", state:"GOOGLE",
                success: { credential, response, parameters in
                    print(credential.oauthToken)
                    // Do your request
                },
                failure: { error in
                    print(error.localizedDescription)
                    print((error as OAuthSwiftError).errorUserInfo)
                }
        )
    

    but the result always failed

    The operation couldn’t be completed. (OAuthSwiftError error -11.)
    [“request”: https://www.googleapis.com/oauth2/v4/token, “error”: Error
    Domain=NSURLErrorDomain Code=401 “invalid_client Unauthorized”
    UserInfo={Response-Body={ “error”: “invalid_client”,
    “error_description”: “Unauthorized” } ,
    NSErrorFailingURLKey=https://www.googleapis.com/oauth2/v4/token,
    Response-Headers={
    “Cache-Control” = “private, max-age=0”;
    “Content-Encoding” = gzip;
    “Content-Length” = 81;
    “Content-Type” = “application/json; charset=UTF-8”;
    Date = “Thu, 15 Jun 2017 04:25:10 GMT”;
    Expires = “Thu, 15 Jun 2017 04:25:10 GMT”;
    Server = GSE;
    Vary = “Origin, X-Origin”;
    “Www-Authenticate” = “Bearer realm=\”https://accounts.google.com/\””;
    “alt-svc” = “quic=\”:443\”; ma=2592000; v=\”38,37,36,35\””;
    “x-content-type-options” = nosniff;
    “x-frame-options” = SAMEORIGIN;
    “x-xss-protection” = “1; mode=block”; }, OAuthSwiftError.response= { URL:
    https://www.googleapis.com/oauth2/v4/token } { status code: 401,
    headers {
    “Cache-Control” = “private, max-age=0”;
    “Content-Encoding” = gzip;
    “Content-Length” = 81;
    “Content-Type” = “application/json; charset=UTF-8”;
    Date = “Thu, 15 Jun 2017 04:25:10 GMT”;
    Expires = “Thu, 15 Jun 2017 04:25:10 GMT”;
    Server = GSE;
    Vary = “Origin, X-Origin”;
    “Www-Authenticate” = “Bearer realm=\”https://accounts.google.com/\””;
    “alt-svc” = “quic=\”:443\”; ma=2592000; v=\”38,37,36,35\””;
    “x-content-type-options” = nosniff;
    “x-frame-options” = SAMEORIGIN;
    “x-xss-protection” = “1; mode=block”; } }, OAuthSwiftError.response.data=<7b0a2022 6572726f 72223a20 22696e76
    616c6964 5f636c69 656e7422 2c0a2022 6572726f 725f6465 73637269
    7074696f 6e223a20 22556e61 7574686f 72697a65 64220a7d 0a>,
    NSLocalizedDescription=invalid_client Unauthorized}]

    Could you please correct it ?

    Solutions Collect From Internet About “Google OAuth 2.0 invalid_client Unauthorized”

    You must remove replace withCallbackURL "com.googleusercontent.apps.984813079630-f828a92sqtl5lgumd4kgp9i30bs9og09:/oauth" with "your.bundle.id:/oauth2Callback"

    Commentary from demo OAuthSwift:

    For googgle the redirect_uri should match your this syntax:
    your.bundle.id:/oauth2Callback

    In plist define a url schem with: your.bundle.id

    After these changes, I still had this problem. Found solution:

    I removed the customerSecret by an empty string then.. it worked.

    I hope to help you with my answer above!