gpt4 book ai didi

ios - 在 iOS 应用程序中实现 OAuth 1.0

转载 作者:可可西里 更新时间:2023-11-01 03:06:42 30 4
gpt4 key购买 nike

我一整天都在为这件事伤脑筋。

我希望将我的 iOS 应用与 Withings api 集成。它使用 OAuth 1.0,我似乎无法完全理解如何实现它。

我一直在下载多个 OAuth 框架(MPOAuthgtm-oauthssoauthkit),但无法完全弄清楚我到底应该做什么。

我进行了很多搜索,也在 stack overflow 中搜索了关于如何总体上实现 OAuth 1.0 以及与 Withings 集成的良好引用,但没有成功。

请解释将 iOS 应用程序与需要 OAuth 1.0 的 api 集成的流程。代码示例将非常有帮助。建议的第 3 方框架也不错。

澄清一下,我完全理解 OAuth 1.0 原则,只是在我的应用中实际实现它时遇到问题。

我认为带有代码示例和良好引用的详尽答案对很多人都非常有帮助,因为我找不到。如果有人有很好的实现经验,请花时间分享。

最佳答案

TDOAuth在我看来是最好的解决方案。它简洁明了,只有一个 .h 和 .m 文件可以使用,没有复杂的示例项目..

这是 OAuth 1.0 流程:

第 1 步 - 获取请求 token

//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:&response error:&error];
NSString *s = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
//parse result
NSMutableDictionary *params = [NSMutableDictionary dictionary];
NSArray *split = [s componentsSeparatedByString:@"&"];
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"];

第 2 步 - 获取授权 token (通过在 UIWebView 中加载请求,webViewDidFinishLoad 委托(delegate)方法将处理回调..)

//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];

按照以下方式处理 webView 以启动第 3 步(我知道 isAuthorizeCallBack 有很多味道,但它确实起作用了,应该对其进行重构..)

- (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:@"&"];
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];
}

最后 - 第 3 步 - 获取访问 token

- (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:&response error:&error];
NSString *s = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];

//parse result
NSMutableDictionary *params = [NSMutableDictionary dictionary];
NSArray *split = [s componentsSeparatedByString:@"&"];
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"]];
}

关于ios - 在 iOS 应用程序中实现 OAuth 1.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15930628/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com