Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Trouble correctly formatting Header Dict. Error "Invalid authorization header format."

Hello. I've read over the documentation a hundred times and tried a bunch of different things but I've been getting the same error for a while now. I don't think the documentation is very clear at all about what is expected from the header. Most of examples I have read online ignore implementing a header completely but apparently that isn't an option for me because the only errors I get are related the error.

 

I am using https://www.base64encode.org/ and encoding "client_id:client_secret" to get my encoded authorization string which gives me a 52 character string. 

 

I am using the OAuthSwift Library and my code looks like:

 NSDictionary *parameters = @{
                                 @"consumerKey": @"MYKEY",
                                 @"consumerSecret": @"MYSECRET",
                                 @"authorizeUrl": @"https://www.fitbit.com/oauth2/authorize",
                                 @"accessTokenUrl": @"https://api.fitbit.com/oauth2/token",
                                 @"responseType" : @"code"
                                 };
    
    NSDictionary *headers = @{
                              @"Authorization": @"Basic my encoded string",
                              @"Content-Type:":@"application/x-www-form-urlencoded",
                              };
...
OAuth2Swift *oauth = [[OAuth2Swift alloc] initWithParameters:parameters];
[oauth objc_authorizeWithCallbackURL:@"CALLBACKURL" scope:@"activity" state:@"FITBIT" parameters:parameters headers:headers
                                 success:^(OAuthSwiftCredential *credential, OAuthSwiftResponse *response, NSDictionary<NSString *,id> *parameters) {
                                     NSString *userId = parameters[@"userid"];
                                     _userIDStr = userId;
                                     
                                     NSLog(@"SUCSESS: %@",userId);
                                 }
                                 failure:^(NSError *error) {
                                     
                                     NSLog(@"FAILURE with error: %@", error);
                                 }];

The error in the xcode terminal reads:

error: Error Domain=OAuthSwiftError Code=-11 "(null)" UserInfo={request=<NSMutableURLRequest: 0x608000202ff0> { URL: https://api.fitbit.com/oauth2/token }, error=Error Domain=NSURLErrorDomain Code=401 "" UserInfo={Response-Body={"errors":[{"errorType":"invalid_client","message":"Invalid authorization header format. Visit https://dev.fitbit.com/docs/oauth2 for more information on the Fitbit Web API authorization process."}],"success":false}, NSErrorFailingURLKey=https://api.fitbit.com/oauth2/token, Response-Headers={
    "Cache-Control" = "no-cache, private";
    "Content-Encoding" = gzip;
    "Content-Language" = "en-US";
    "Content-Type" = "application/json;charset=utf-8";
    Date = "Fri, 14 Jul 2017 21:23:30 GMT";
    Server = "cloudflare-nginx";
    Vary = "Origin,Accept-Encoding";
    "Www-Authenticate" = "Bearer realm=\"api.fitbit.com\"";
    "cf-ray" = "37e78b25ae285005-DEN";
    "x-frame-options" = SAMEORIGIN;
}, OAuthSwiftError.response=<NSHTTPURLResponse: 0x60800023df60> { URL: https://api.fitbit.com/oauth2/token } { status code: 401, headers {
    "Cache-Control" = "no-cache, private";
    "Content-Encoding" = gzip;
    "Content-Language" = "en-US";
    "Content-Type" = "application/json;charset=utf-8";
    Date = "Fri, 14 Jul 2017 21:23:30 GMT";
    Server = "cloudflare-nginx";
    Vary = "Origin,Accept-Encoding";
    "Www-Authenticate" = "Bearer realm=\"api.fitbit.com\"";
    "cf-ray" = "37e78b25ae285005-DEN";
    "x-frame-options" = SAMEORIGIN;
} }, OAuthSwiftError.response.data=<7b226572 726f7273 223a5b7b 22657272 6f725479 7065223a 22696e76 616c6964 5f636c69 656e7422 2c226d65 73736167 65223a22 496e7661 6c696420 61757468 6f72697a 6174696f 6e206865 61646572 20666f72 6d61742e 20566973 69742068 74747073 3a2f2f64 65762e66 69746269 742e636f 6d2f646f 63732f6f 61757468 3220666f 72206d6f 72652069 6e666f72 6d617469 6f6e206f 6e207468 65204669 74626974 20576562 20415049 20617574 686f7269 7a617469 6f6e2070 726f6365 73732e22 7d5d2c22 73756363 65737322 3a66616c 73657d>, NSLocalizedDescription=}}

 Any advice is really appreciated. I am not sure where to go from here and have not found information on how to properly format these headers online. Thank you!

Best Answer
0 Votes
0 REPLIES 0