【问题标题】:How To Use OAuth in My iPhone App?如何在我的 iPhone 应用程序中使用 OAuth?
【发布时间】:2011-06-28 21:36:20
【问题描述】:

我正在尝试将 OAuth 配置到我的 iPhone 应用中以连接到另一个 Web 服务,但我遇到了问题。

我已经下载了 google gtm-oauth 文件并将它们添加到我的项目中。

- (GTMOAuthAuthentication *)myCustomAuth {
    NSString *myConsumerKey = @"2342343242";    // pre-registered with service
    NSString *myConsumerSecret = @"324234234242"; // pre-assigned by service

    GTMOAuthAuthentication *auth;
    auth = [[[GTMOAuthAuthentication alloc] initWithSignatureMethod:kGTMOAuthSignatureMethodHMAC_SHA1
                                                        consumerKey:myConsumerKey
                                                         privateKey:myConsumerSecret] autorelease];

    // setting the service name lets us inspect the auth object later to know
    // what service it is for
    auth.serviceProvider = @"RunKeeper";

    return auth;
}
- (void)signInToCustomService {

    NSURL *requestURL = [NSURL URLWithString:@"https://runkeeper.com/apps/token"];
    NSURL *accessURL = [NSURL URLWithString:@"https://runkeeper.com/apps/token"];
    NSURL *authorizeURL = [NSURL URLWithString:@"https://runkeeper.com/apps/authorize"];
    NSString *scope = @"http://example.com/scope";

    GTMOAuthAuthentication *auth = [self myCustomAuth];

    // set the callback URL to which the site should redirect, and for which
    // the OAuth controller should look to determine when sign-in has
    // finished or been canceled
    //
    // This URL does not need to be for an actual web page
    [auth setCallback:@"http://www.example.com/OAuthCallback"];

    // Display the autentication view
    GTMOAuthViewControllerTouch *viewController;
    viewController = [[[GTMOAuthViewControllerTouch alloc] initWithScope:scope
                                                                language:nil
                                                         requestTokenURL:requestURL
                                                       authorizeTokenURL:authorizeURL
                                                          accessTokenURL:accessURL
                                                          authentication:auth
                                                          appServiceName:@"RunKeeper"
                                                                delegate:self
                                                        finishedSelector:@selector(viewController:finishedWithAuth:error:)] autorelease];

    [[self navigationController] pushViewController:viewController
                                           animated:YES];
}

这是我尝试连接的 API:http://developer.runkeeper.com/healthgraph/registration-authorization

【问题讨论】:

    标签: iphone objective-c xcode json oauth


    【解决方案1】:

    首先,阅读 oauth。它与您正常的登录/密码类型验证有点不同。

    其次,你可以在http://code.google.com/p/gtm-oauth/使用谷歌的oauth库

    【讨论】:

    • 好的,我下载了所有相关文件并将它们添加到我的项目中的文件夹中,但是现在我该怎么办?
    猜你喜欢
    • 2010-11-08
    • 2011-04-27
    • 2011-12-07
    • 1970-01-01
    • 2011-04-09
    • 1970-01-01
    • 2011-06-06
    • 2011-04-12
    • 2012-08-16
    相关资源
    最近更新 更多