【问题标题】:How to fetch all Google Contacts in iOS 10如何在 iOS 10 中获取所有 Google 联系人
【发布时间】:2016-12-05 06:02:50
【问题描述】:

我尝试使用 Google Contacts APi 获取所有 google 联系人,但我没有发现任何适用于 iOS 的东西。 OAuth 不起作用,因为 google 更改了使用 WebView OAuth 的策略。我使用谷歌登录框架获得访问令牌,但这个令牌对谷歌联系人 API 无效: https://www.google.com/m8/feeds/contacts/default/full.

请建议使用最新的 iOS SDK 10+ 和最新的 google 框架来获取 google/gmail 联系人

【问题讨论】:

    标签: ios ios10 google-contacts-api


    【解决方案1】:

    尝试使用 iOS 的 GIDSignIn Class 来处理您的 OAuth 问题。

    请参阅"Google Sign-In for iOS" 这是使用 GIDSignIn 的示例代码:

    1. 获取对 GIDSignIn 共享实例的引用:GIDSignIn *signIn = [GIDSignIn sharedInstance];
    2. 设置您要请求的 OAuth 2.0 范围:[signIn setScopes:[NSArray arrayWithObject:@"https://www.googleapis.com/auth/plus.login"]];
    3. 调用 [signIn setDelegate:self];
    4. 设置委托方法signIn:didSignInForUser:withError:.
    5. 从应用程序委托中的 application:openUrl:... 调用共享实例上的 handleURL。
    6. 在共享实例上调用登录;

    您也可以查看此SO thread 了解更多信息。

    【讨论】:

      【解决方案2】:

      我)尝试GIDSignIn Class 进行身份验证:

      II ) 导入所需的类作为休闲:

      #import <UIKit/UIKit.h>
      #import <GoogleSignIn/GoogleSignIn.h>    
      @interface ViewController : UIViewController<GIDSignInDelegate,GIDSignInUIDelegate,NSURLSessionDelegate>
      
      @end
      

      III) 在 vi​​ewDidLoad 中做休闲:

      - (void)viewDidLoad {
          [super viewDidLoad];
      
          [GIDSignIn sharedInstance].delegate = self;
          [GIDSignIn sharedInstance].uiDelegate = self;
          [GIDSignIn sharedInstance].clientID = "your_CLIENT_ID; //get from console
          [[GIDSignIn sharedInstance] setScopes:@[@"https://www.googleapis.com/auth/plus.login",@"https://www.googleapis.com/auth/plus.me",@"https://www.googleapis.com/auth/contacts",@"https://www.googleapis.com/auth/contacts.readonly"]];
      
          // Button for opening Google Auth //
      
          GIDSignInButton *button = [[GIDSignInButton alloc]initWithFrame:CGRectMake(0.0f, 0.0f, 200.0f, 200.0f)];
          button.style = kGIDSignInButtonStyleWide;
          button.colorScheme  =kGIDSignInButtonColorSchemeLight;
          [button setCenter:self.view.center];
          [self.view addSubview:button];
      }
      

      IV ) 用户完成登录后立即通过GIDSignInUIDelegate 处理如下:

      - (void)signIn:(GIDSignIn *)signIn didSignInForUser:(GIDGoogleUser *)user withError:(NSError *)error {
      
          // Perform any operations on signed in user here.
          NSString *userId = user.userID;                  // For client-side use only!
          NSString *idToken = user.authentication.idToken; // Safe to send to the server
          NSString *fullName = user.profile.name;
          NSString *givenName = user.profile.givenName;
          NSString *familyName = user.profile.familyName;
          NSString *email = user.profile.email;
      
          [self getPeopleList];
          // ...
      }
      // Implement these methods only if the GIDSignInUIDelegate is not a subclass of
      // UIViewController.
      
      // Stop the UIActivityIndicatorView animation that was started when the user
      // pressed the Sign In button
      - (void)signInWillDispatch:(GIDSignIn *)signIn error:(NSError *)error {
          NSLog(@"0.0.");
      
      }
      
      // Present a view that prompts the user to sign in with Google
      - (void)signIn:(GIDSignIn *)signIn presentViewController:(UIViewController *)viewController {
          [self presentViewController:viewController animated:YES completion:nil];
      }
      
      // Dismiss the "Sign in with Google" view
      - (void)signIn:(GIDSignIn *)signIn dismissViewController:(UIViewController *)viewController {
          [self dismissViewControllerAnimated:YES completion:nil];
      }
      

      V) 现在在-(void)getPeopleList{} 你会找到联系人:

      -(void)getPeopleList{
      
          NSString *url = [NSString stringWithFormat:@"https://people.googleapis.com/v1/people/me/connections?personFields=emailAddresses"];
      
          NSURL *linkurl = [NSURL URLWithString:url];
      
      
          NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
          NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
          NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:linkurl
                                                                 cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                             timeoutInterval:60.0];
      
          [request addValue:@"application/json" forHTTPHeaderField:@"Accept"];
          [request addValue:[NSString stringWithFormat:@"Bearer %@",[GIDSignIn sharedInstance].currentUser.authentication.accessToken] forHTTPHeaderField:@"Authorization"];
          [request setHTTPMethod:@"GET"];
      
          NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
      
              if (!error) {
                  NSDictionary *user_contact_json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
                  NSLog(@"user_contacts %@", user_contact_json);
              } else {
                  NSLog(@"Error %@",error);
              }
          }];
      
          [postDataTask resume];
      
      }
      

      【讨论】:

        猜你喜欢
        • 2016-03-02
        • 2017-04-04
        • 1970-01-01
        • 2015-01-08
        • 1970-01-01
        • 1970-01-01
        • 2011-04-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多