Google Contacts API version 3.0 to retrieve all contacts for objective C (iOS) sample request format?

978 Views Asked by At

In my application user logs in throgh google+ credentials for which I have implemented google+ framework for authentication/authorization. After Successful login I have to get the contacts list of the logged in user.

for which I am using Google Contacts API version 3.0 url https://www.google.com/m8/feeds/contacts/default/full

After GPP Sign In authentication request..

- (void)finishedWithAuth:(GTMOAuth2Authentication *)auth
                   error:(NSError *)error {
    if (error) {
        NSLog(@"%@",[NSString stringWithFormat:@"Status: Authentication error: %@", error]);
        return;
    }

    auth.clientID  =@"XXXX.apps.googleusercontent.com";
    auth.clientSecret  =@"xxxxxxxx-z7tmOqQCShgH3ax";


    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.scope= @"https://www.googleapis.com/auth/contacts.readonly";
    [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];
                 }
             }
         }];
}

in the successful response I have written code like this.

But the output is not as desired. The output string is huge junk value and at the end of it is showing

401. That's an error.

There was an error in your request. That's all we know.

Could any one please help in resolving the issue.

1

There are 1 best solutions below

1
On

401 means that you have not correctly authorized. I don't work with that api but it's possible your credentials are incorrect.

you might also be seeing certificate problems for the https? you should set up this call back and see if it provides more info.

How to use NSURLConnection to connect with SSL for an untrusted cert?