How to fetch gmail Contacts in iOS application using google contacts api?

前端 未结 1 1110
故里飘歌
故里飘歌 2020-12-17 00:47

In my application we kept option to login through gmail. I have requirement to retrieve gmail contacts.

In the following method i am using auth object(once success)

相关标签:
1条回答
  • 2020-12-17 01:47

    The correct Scope is "https://www.google.com/m8/feeds"

    In swift

    class func getContactsFromUser() {
            let urlStr = "https://www.google.com/m8/feeds/contacts/default/full"
            let url = NSURL(string: urlStr);
    
            var request = NSMutableURLRequest(URL: url!)
    
            let appd = UIApplication.sharedApplication().delegate as! AppDelegate
            let error: NSError!
            appd.service.authorizer.authorizeRequest!(request, completionHandler: { (error) -> Void in
    
                if error != nil {
                    println("error getting contacts is \(error.localizedDescription)")
                } else {
                    let response: AutoreleasingUnsafeMutablePointer<NSURLResponse?>=nil
    
                    let data = NSURLConnection.sendSynchronousRequest(request, returningResponse: response, error: nil)
    
                    if data != nil {
                        let stringResponse = NSString(data: data!, encoding: NSUTF8StringEncoding)
                        println("**** stringResponse **** \(stringResponse!)")
                    } else {
                        println("error 2 getting contacts is ")
                    }
                }
            })
        }
    

    In objective c

    - (void)doAnAuthenticatedAPIFetch {
    NSString *urlStr = @"https://www.google.com/m8/feeds/contacts/default/full";
    NSURL *url = [NSURL URLWithString:urlStr];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [self.auth authorizeRequest:request
              completionHandler:^(NSError *error) {
                  NSString *output = nil;
                  if (error) {
                      output = [error description];
                  } else {
                      NSURLResponse *response = nil;
                      NSData *data = [NSURLConnection sendSynchronousRequest:request
                                                           returningResponse:&response
                                                                       error:&error];
                      if (data) {
                          // API fetch succeeded :Here I am getti
                          output = [[NSString alloc] initWithData:data
                                                         encoding:NSUTF8StringEncoding];
                      } else {
                          // fetch failed
                          output = [error description];
                      }
                  }
              }];
    }
    
    0 讨论(0)
提交回复
热议问题