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

前端之家收集整理的这篇文章主要介绍了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应用程序,任何示例应用程序,可用的代码段中使用这些?

请帮我.

这是我的代码

  1. -(void) loadGmail_Login
  2. {
  3. NSString *keychainItemName = nil;
  4. if ([self shouldSaveInKeychain]) {
  5. keychainItemName = kKeychainItemName;
  6. }
  7.  
  8. // For GTM applications,the scope is available as
  9. NSString *scope = @"http://www.google.com/m8/Feeds/";
  10.  
  11. // ### Important ###
  12. // GTMOAuthViewControllerTouch is not designed to be reused. Make a new
  13. // one each time you are going to show it.
  14.  
  15. // Display the autentication view.
  16. GTMOAuthAuthentication *auth;
  17. auth = [GTMOAuthViewControllerTouch authForGoogleFromKeychainForName:kKeychainItemName];
  18.  
  19. GTMOAuthViewControllerTouch *viewController = [[[GTMOAuthViewControllerTouch alloc]
  20. initWithScope:scope
  21. language:nil
  22. appServiceName:keychainItemName
  23. delegate:self
  24. finishedSelector:@selector(viewController:finishedWithAuth:error:)] autorelease];
  25.  
  26. // You can set the title of the navigationItem of the controller here,if you want.
  27. // Optional: display some html briefly before the sign-in page loads
  28. NSString *html = @"<html><body bgcolor=silver><div align=center>Loading sign-in page...</div></body></html>";
  29. [viewController setInitialHTMLString:html];
  30.  
  31. [[self navigationController] pushViewController:viewController animated:YES];
  32.  
  33. }
  34.  
  35. - (void)viewController:(GTMOAuthViewControllerTouch *)viewController
  36. finishedWithAuth:(GTMOAuthAuthentication *)auth
  37. error:(NSError *)error
  38. {
  39. if (error != nil)
  40. {
  41. // Authentication Failed (perhaps the user denied access,or closed the
  42. // window before granting access)
  43. NSLog(@"Authentication error: %@",error);
  44. NSData *responseData = [[error userInfo] objectForKey:@"data"]; // kGTMHTTPFetcherStatusDataKey
  45. if ([responseData length] > 0) {
  46. // show the body of the server's authentication failure response
  47. NSString *str = [[[NSString alloc] initWithData:responseData
  48. encoding:NSUTF8StringEncoding] autorelease];
  49. NSLog(@"%@",str);
  50. }
  51.  
  52. [self setAuthentication:nil];
  53. }
  54. else
  55. {
  56. // save the authentication object
  57. [self setAuthentication:auth];
  58.  
  59. // Just to prove we're signed in,we'll attempt an authenticated fetch for the
  60. // signed-in user
  61. [self doAnAuthenticatedAPIFetch];
  62. }
  63.  
  64. }
  65.  
  66. - (void)doAnAuthenticatedAPIFetch
  67. {
  68. NSString *urlStr;
  69.  
  70. // Google Contacts Feed
  71. //
  72. // https://www.googleapis.com/oauth2/v2/userinfo
  73. urlStr = @"http://www.google.com/m8/Feeds/contacts/default/thin";
  74.  
  75. NSURL *url = [NSURL URLWithString:urlStr];
  76. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
  77. [mAuth authorizeRequest:request];
  78.  
  79. NSError *error = nil;
  80. NSURLResponse *response = nil;
  81. NSData *data = [NSURLConnection sendSynchronousRequest:request
  82. returningResponse:&response
  83. error:&error];
  84. if (data) {
  85. // API fetch succeeded
  86. NSString *str = [[[NSString alloc] initWithData:data
  87. encoding:NSUTF8StringEncoding] autorelease];
  88. NSLog(@"API response: %@",str);
  89.  
  90. GGCXml_Adaptor *localAlphabetXMLParser = [[GGCXml_Adaptor alloc] init];
  91. [localAlphabetXMLParser processBooksXML:data];
  92. [localAlphabetXMLParser release];
  93. // [self updateUI];
  94.  
  95.  
  96.  
  97. } else {
  98. // fetch Failed
  99. NSLog(@"API fetch error: %@",error);
  100. }
  101. }
  102.  
  103. - (void)setAuthentication:(GTMOAuthAuthentication *)auth {
  104. [mAuth autorelease];
  105. mAuth = [auth retain];
  106. }

解决方法

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

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

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

猜你在找的iOS相关文章