01-11-2016 15:47 - edited 01-12-2016 09:35
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

01-11-2016 15:47 - edited 01-12-2016 09:35
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Hi,
I am trying to use OAuth2 for one of my ios application.
I am pretty sure I am setting the Authorization corretly and then using the authentication corretly and here is the response i get from server:
My Authorization request header are :
"Accept-Language" = "en-US;q=1";
Authorization = "Basic MjI3SE5DOjAwN*****";
"User-Agent" = "****";
Then my paramerters to fitbit are:
"client_id" = ***;
"redirect_uri" = "example://";
"response_type" = code;
scope = activity;
base URL is : https://www.fitbit.com/oauth2/authorize
and I am getting this error:
Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html" UserInfo={com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x155f4e270> { URL: https://www.fitbit.com/login?redirect=%2Foauth2%2Fauthorize%3Fnull } { status code: 200, headers {
"Cache-Control" = "no-store, no-cache, must-revalidate";
"Content-Encoding" = gzip;
"Content-Language" = "en-US";
"Content-Type" = "text/html;charset=UTF-8";
Date = "Mon, 11 Jan 2016 22:30:04 GMT";
Expires = "Thu, 01 Jan 1970 00:00:00 GMT";
Pragma = "no-cache";
Server = "cloudflare-nginx";
"Set-Cookie" = "fhttps=\"\"; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Path=/, JSESSIONID=5FAD4DAE5C8C54329EA14FA236BE502E.fitbit1; Path=/; HttpOnly";
Vary = "Accept-Encoding";
"cf-ray" = "26341067ec7e0da9-SJC";
"x-frame-options" = SAMEORIGIN;
"x-ua-compatible" = "IE=edge,chrome=1";
} }, NSErrorFailingURLKey=https://www.fitbit.com/login?redirect=%2Foauth2%2Fauthorize%3Fnull, com.alamofire.serialization.response.error.data=<0a0a0a0a 0a0a0a0a 0a0a0a0a 0a0a0a0a 0a0a0a0a 0a0a0a0a 0a3c2144 4f435459 50452068 746d6c3e 0a3c212d 2d5b6966 206c7420 49452037 5d3e3c68 746d6c20 636c6173 733d226e 6f2d6a73 20696536 20696522 3e3c215b 656e6469 665d2d2d 3e0a3c21 2d2d5b69 66204945 20375d3e 2020203c 68746d6c 20636c61 73733d22 6e6f2d6a 73206965 37206965 223e3c21 5b656e64 69665d2d 2d3e0a3c 212d2d5b 69662049 4520385d 3e202020 3c68746d 6c20636c 6173733d 226e6f2d 6a732069 65382069 65223e3c 21....a lot of data stream i need to figure out how to decode it.
And When I start accepting the text/html I get this error:
Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
From the first try I guess the reponse has this string" https://www.fitbit.com/login?redirect=%2Foauth2%2Fauthorize%3Fnull" which is not clear to me why its happening.
My Thought is once this goes well I will be redirected automatically to safari where I will be presented with the authenticaion page and I will enter my login details and then redirect to my urlSheme which will launch my app from safari and then the response will contain the access token that i can use further for api calls.
Please help me as I am trying to figure this out since morning.
I also tried this from Postman app and I get a very long "Access Token data as follows:"
<!DOCTYPE html> <html> <head> <meta charset → "utf-8"/> <title>Postman</title> <script type="text/javascript" src="js/config.js"></script> <link rel="stylesheet" type="text/css" href="css/preload.css"/> <link rel="stylesheet" type="text/css" href="css/custom-theme/jquery-ui-1.9.2.custom.min.css"/> <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/> <link rel="stylesheet" type="text/css" href="css/bootstrap-modal.css"/> <link rel="stylesheet" type="text/css" href="css/nanoscroller.css"/> <!--<link rel="stylesheet" type="text/css" href="css/bootstrap-editable.css"/>--> <!-- Xml tree --> <link rel="stylesheet" type="text/css" href="css/xmltree.css" /> <!-- Json Tree --> <link...
Feedback:
I feel the documentation provided by fitbit should be made more specific depending on most user stuck in which kind of environment. I see that people are making iOS apps a lot and fitbit should provide some kind of example or sample code for starting up quickly and investing more on app logic then figuring out the Authorization stuff.
Regards..
Amit
01-11-2016 22:15
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

01-11-2016 22:15
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Could you paste the link what you try to open with the ap to authorize the user?
David

01-12-2016 09:34
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

01-12-2016 09:34
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Thanks for the reply.
The link is the baseurl and FYI I am using AFNetwroking And AFOAuth2Manager.
https://www.fitbit.com/oauth2/authorize
Please let me know if something is wrong with the URL itself.

01-12-2016 10:28
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

01-12-2016 10:28
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
As I did not use any 3rd library when connected to the api, can not suggest something to check. I only created the url at runtime, and parsed the result.

01-12-2016 10:42
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

01-12-2016 10:42
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Thanks it would be great if you could provide the sample of the URL created at runtime:
1. for Basic Authorization that has the cleint key : secret key
2. the URL that send authorization call to fitbit,
I just wanted to check how above two things get combined.
Thanks.
AJ

01-12-2016 15:20
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post



01-12-2016 15:20
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
@ajonios wrote:
Feedback:
I feel the documentation provided by fitbit should be made more specific depending on most user stuck in which kind of environment. I see that people are making iOS apps a lot and fitbit should provide some kind of example or sample code for starting up quickly and investing more on app logic then figuring out the Authorization stuff.
I appreciate the feedback and I'm sorry that you had difficulty. The Fitbit Web API uses a standard implementation of OAuth 2.0. While we wish we could provide support for every platform, we don't have a huge team that is an expert in every language and platform (though we are hiring!). We're very happy to help debug at the HTTP request level, but you may find better answers about generic OAuth 2.0 implementations with iOS on iOS specific support forums.
01-14-2016 10:41 - edited 01-14-2016 10:45
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

01-14-2016 10:41 - edited 01-14-2016 10:45
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
You shouldn't be doing the authorization step using AFOAuth2Manager. The first step of the OAuth dance should be to launch safari (or UIWebView) and creating the link manually.
Before you do this, you will need to create a custom URL scheme in your project that will allow you app to be launched via a URL. This needs to match the external link you speficied in your Fitbit setup.
NSString *urlString = [NSString stringWithFormat:@"https://fitbit.com/oauth2/authorize?scope=<YourScope>&redirect_uri=<YourAppsUrlScheme>&client_id=<YourClientID>&response_type=<TheResponseTypeYouWant>]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
Then, once you have done that an done the authorization in the external safari link, it will launch your app.
In your app's application delegate, implement the following method:
-(BOOL)application:(UIApplication *)application openURL:(nonnull NSURL *)url options:(nonnull NSDictionary<NSString *,id> *)options
Inside this method is where you need to do all your AFOAuth2Manager stuff to continue the process. The url paramterter will contain the code/token you should use to pass to AFOAuth2Manager's authenticateUsingOAuthWithURLString method.
01-14-2016 15:33 - edited 01-14-2016 15:33
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

01-14-2016 15:33 - edited 01-14-2016 15:33
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
@BaxpinThanks a lot for some specific reply to my question :).
I will look into this and accept your answer asa it works.
-AJ

05-19-2016 10:58
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

05-19-2016 10:58
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
I totally agree with this arjonios! I am stuck with the authorization for ios in xcode swift. I have no idea what to do. ..
