繁体   English   中英

如何在iOS 10中获取所有Google联系人

[英]How to fetch all Google Contacts in iOS 10

我试图使用Google Contacts APi获取所有的google联系人,但找不到适用于iOS的任何东西。 由于Google更改了使用WebView OAuth的政策,因此OAuth无法正常工作。 我使用Google登录框架获得了访问令牌,但是此令牌在Google联系人API中无效: https : //www.google.com/m8/feeds/contacts/default/full

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

尝试使用iOS的GIDSignIn类解决您的OAuth问题。

请参阅“ iOS的Google登录”,这是使用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. 在共享实例上调用signIn;

您还可以检查此SO线程以获取其他信息。

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

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

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

@end

III)在viewDidLoad中做为休闲:

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

}

用户联系人图像

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM