ios – 使用带有OAuth 2.0的Google API在iPhone中登录Gmail

我找到了Google提供的服务,可以访问各种Google服务的Google Api.我可以在iPhone中设置一个项目,并为iOS应用程序(通过OAuth2.0)和本机应用程序创建API访问.我想为我的iPhone应用程序使用本机API.它API为我提供了电子邮件,全名,名字,姓氏,google_id,性别,dob,profile_image.如何在我的iPhone应用程序,任何示例应用程序,可用的代码段中使用这些?

请帮我.

这是我的代码

-(void) loadGmail_Login
{
    Nsstring *keychainItemName = nil;
    if ([self shouldSaveInKeychain]) {
        keychainItemName = kKeychainItemName;
    }

    // For GTM applications,the scope is available as
    Nsstring *scope = @"http://www.google.com/m8/Feeds/";

    // ### Important ###
    // GTMOAuthViewControllerTouch is not designed to be reused. Make a new
    // one each time you are going to show it.

    // display the autentication view.
    GTMOAuthAuthentication *auth;
    auth = [GTMOAuthViewControllerTouch authForGoogleFromKeychainForName:kKeychainItemName];

    GTMOAuthViewControllerTouch *viewController = [[[GTMOAuthViewControllerTouch alloc]
                                                    initWithScope:scope
                                                    language:nil
                                                    appServiceName:keychainItemName
                                                    delegate:self
                                                    finishedSelector:@selector(viewController:finishedWithAuth:error:)] autorelease];

    // You can set the title of the navigationItem of the controller here,if you want.
    // Optional: display some html briefly before the sign-in page loads
    Nsstring *html = @"<html><body bgcolor=silver><div align=center>Loading sign-in page...</div></body></html>";
    [viewController setinitialHTMLString:html];

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

}

- (void)viewController:(GTMOAuthViewControllerTouch *)viewController
      finishedWithAuth:(GTMOAuthAuthentication *)auth
                 error:(NSError *)error
{
    if (error != nil)
    {
        // Authentication Failed (perhaps the user denied access,or closed the
        // window before granting access)
        NSLog(@"Authentication error: %@",error);
        NSData *responseData = [[error userInfo] objectForKey:@"data"]; // kGTMHTTPFetcherStatusDataKey
        if ([responseData length] > 0) {
            // show the body of the server's authentication failure response
            Nsstring *str = [[[Nsstring alloc] initWithData:responseData
                                                   encoding:NSUTF8StringEncoding] autorelease];
            NSLog(@"%@",str);
        }

        [self setAuthentication:nil];
    }
    else
    {
        // save the authentication object
        [self setAuthentication:auth];

        // Just to prove we're signed in,we'll attempt an authenticated fetch for the
        // signed-in user
        [self doAnAuthenticatedAPIFetch];
    }

}

- (void)doAnAuthenticatedAPIFetch
{
    Nsstring *urlStr;

    // Google Contacts Feed
    //
    //    https://www.googleapis.com/oauth2/v2/userinfo
    urlStr = @"http://www.google.com/m8/Feeds/contacts/default/thin";

    NSURL *url = [NSURL URLWithString:urlStr];
    NSMutableuRLRequest *request = [NSMutableuRLRequest requestWithURL:url];
    [mAuth authorizeRequest:request];

    NSError *error = nil;
    NSURLResponse *response = nil;
    NSData *data = [NSURLConnection sendSynchronousRequest:request
                                         returningResponse:&response
                                                     error:&error];
    if (data) {
        // API fetch succeeded
        Nsstring *str = [[[Nsstring alloc] initWithData:data
                                               encoding:NSUTF8StringEncoding] autorelease];
        NSLog(@"API response: %@",str);

        GGCXml_Adaptor *localAlphabetXMLParser = [[GGCXml_Adaptor alloc] init];
        [localAlphabetXMLParser processBooksXML:data];
        [localAlphabetXMLParser release];
        //        [self updateUI];



    } else {
        // fetch Failed
        NSLog(@"API fetch error: %@",error);
    }
}

- (void)setAuthentication:(GTMOAuthAuthentication *)auth {
    [mAuth autorelease];
    mAuth = [auth retain];
}

解决方法

首先,您需要从Google Api获取令牌,对于第一步,您必须遵循本教程,并在此链接的末尾有完整的iOS源代码,用于从Google Api获取令牌

http://technogerms.com/login-with-google-using-oauth-2-0-for-ios-xcode-objective-c/

然后在下一步中,您必须将该令牌发送到Google Api以请求用户数据,我只需要第一步所以我正在分享我的搜索

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...