Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
470 views
in Technique[技术] by (71.8m points)

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

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) to fetch gmail contacts by creating request with url: "https://www.google.com/m8/feeds/contacts/default/full"

- (void)finishedWithAuth:(GTMOAuth2Authentication *)auth
               error:(NSError *)error {
if(!error) {

auth.clientID  =myClientId;
auth.clientSecret  =myClientSecret;
auth.scope= @"https://www.googleapis.com/auth/contacts.readonly";

NSString *urlStr = @"https://www.google.com/m8/feeds/contacts/default/full";

NSURL *url = [NSURL URLWithString:urlStr];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"GET"];
[request setValue:@"3.0" forHTTPHeaderField:@"GData-Version"];
[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];
                      NSLog(@"%@",output);
                  } else {
                      // fetch failed
                      output = [error description];
                  }
              }
          }];
 }
}

I'm getting client error(401). is there any thing i'm missing to my request.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...