Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="https://github.com/tweetdeck/TDOAuth" rel="noreferrer">TDOAuth</a> in my opinion was the best solution. it is clean and simple, only one .h and .m file to work with, and no complicated example projects..</p> <p>This is the OAuth 1.0 flow:</p> <p><strong>step 1 - get request token</strong> </p> <pre><code>//withings additional params NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject:CALL_BACK_URL forKey:@"oauth_callback"]; //init request NSURLRequest *rq = [TDOAuth URLRequestForPath:@"/request_token" GETParameters:dict scheme:@"https" host:@"oauth.withings.com/account" consumerKey:WITHINGS_OAUTH_KEY consumerSecret:WITHINGS_OAUTH_SECRET accessToken:nil tokenSecret:nil]; //fire request NSURLResponse* response; NSError* error = nil; NSData* result = [NSURLConnection sendSynchronousRequest:rq returningResponse:&amp;response error:&amp;error]; NSString *s = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding]; //parse result NSMutableDictionary *params = [NSMutableDictionary dictionary]; NSArray *split = [s componentsSeparatedByString:@"&amp;"]; for (NSString *str in split){ NSArray *split2 = [str componentsSeparatedByString:@"="]; [params setObject:split2[1] forKey:split2[0]]; } token = params[@"oauth_token"]; tokenSecret = params[@"oauth_token_secret"]; </code></pre> <p><strong>step 2 - get authorize token</strong> (by loading the request in a UIWebView, the webViewDidFinishLoad delegate method will handle the call back..)</p> <pre><code>//withings additional params NSMutableDictionary *dict2 = [NSMutableDictionary dictionary]; [dict setObject:CALL_BACK_URL forKey:@"oauth_callback"]; //init request NSURLRequest *rq2 = [TDOAuth URLRequestForPath:@"/authorize" GETParameters:dict2 scheme:@"https" host:@"oauth.withings.com/account" consumerKey:WITHINGS_OAUTH_KEY consumerSecret:WITHINGS_OAUTH_SECRET accessToken:token tokenSecret:tokenSecret]; webView.delegate = self; [DBLoaderHUD showDBLoaderInView:webView]; [webView loadRequest:rq2]; </code></pre> <p><strong>handle the webView as follow to initiate step 3</strong> (I know the isAuthorizeCallBack smells a lot, but it does the job, should refactor it..)</p> <pre><code>- (void)webViewDidFinishLoad:(UIWebView *)aWebView { [DBLoaderHUD hideDBLoaderInView:webView]; NSString *userId = [self isAuthorizeCallBack]; if (userId) { //step 3 - get access token [DBLoaderHUD showDBLoaderInView:self.view]; [self getAccessTokenForUserId:userId]; } //ugly patchup to fix an invalid token bug if ([webView.request.URL.absoluteString isEqualToString:@"http://oauth.withings.com/account/authorize?"]) [self startOAuthFlow]; } - (NSString *)isAuthorizeCallBack { NSString *fullUrlString = webView.request.URL.absoluteString; if (!fullUrlString) return nil; NSArray *arr = [fullUrlString componentsSeparatedByString:@"?"]; if (!arr || arr.count!=2) return nil; if (![arr[0] isEqualToString:CALL_BACK_URL]) return nil; NSString *resultString = arr[1]; NSArray *arr2 = [resultString componentsSeparatedByString:@"&amp;"]; if (!arr2 || arr2.count!=3) return nil; NSString *userCred = arr2[0]; NSArray *arr3 = [userCred componentsSeparatedByString:@"="]; if (!arr3 || arr3.count!=2) return nil; if (![arr3[0] isEqualToString:@"userid"]) return nil; return arr3[1]; } - (void)startOAuthFlow { [self step1]; [self step2]; } </code></pre> <p><strong>and finally - step 3 - get access token</strong></p> <pre><code>- (void)getAccessTokenForUserId:(NSString *)userId { //step 3 - get access token //withings additional params NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject:CALL_BACK_URL forKey:@"oauth_callback"]; [dict setObject:userId forKey:@"userid"]; //init request NSURLRequest *rq = [TDOAuth URLRequestForPath:@"/access_token" GETParameters:dict scheme:@"https" host:@"oauth.withings.com/account" consumerKey:WITHINGS_OAUTH_KEY consumerSecret:WITHINGS_OAUTH_SECRET accessToken:token tokenSecret:tokenSecret]; //fire request NSURLResponse* response; NSError* error = nil; NSData* result = [NSURLConnection sendSynchronousRequest:rq returningResponse:&amp;response error:&amp;error]; NSString *s = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding]; //parse result NSMutableDictionary *params = [NSMutableDictionary dictionary]; NSArray *split = [s componentsSeparatedByString:@"&amp;"]; for (NSString *str in split){ NSArray *split2 = [str componentsSeparatedByString:@"="]; [params setObject:split2[1] forKey:split2[0]]; } [self finishedAthourizationProcessWithUserId:userId AccessToken:params[@"oauth_token"] AccessTokenSecret:params[@"oauth_token_secret"]]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload