Having trouble with GData Contact image fetching

120 views
Skip to first unread message

prathumca

unread,
Nov 28, 2012, 11:45:13 PM11/28/12
to gdata-objec...@googlegroups.com
I'm working on a POC and the main idea is to display list of Google contacts (with images) by using GData (version 3.0) + OAuth 2.0. I was able to get all the contacts but while fetching images asynchronously by using GTMHTTPFetcher I get the following error always

The operation couldn’t be completed. (com.google.HTTPStatus error 401.)

I know erros 401 stands unauthorized access, but I've set authorizer to GTMHTTPFetcher object. Please find the below code.

- (GDataServiceGoogleContact *)contactService {
    static GDataServiceGoogleContact* service = nil;
    if (!service) {
        service = [[GDataServiceGoogleContact alloc] init];
        [service setShouldCacheResponseData:YES];
        [service setServiceShouldFollowNextLinks:YES];
        MyAppDelegate *appDelegate = (MyAppDelegate *) [[UIApplication sharedApplication] delegate];
        [service setAuthorizer:appDelegate.auth];
    }
    return service;
}












- (void) loadImageInBackGround:(NSIndexPath*) indexPath {

GDataEntryContact *contact = [[self contacts] objectAtIndex:indexPath.row];

GDataLink *photoLink = [contact photoLink]; // get an NSURLRequest object with an auth token NSURL *imageURL = [photoLink URL]; GDataServiceGoogleContact *service = [self contactService]; NSMutableURLRequest *request = [service requestForURL:imageURL ETag:nil httpMethod:nil]; [request setValue:@"image/*" forHTTPHeaderField:@"Accept"]; GTMHTTPFetcher *fetcher = [GTMHTTPFetcher fetcherWithRequest:request]; [fetcher setAuthorizer:[service authorizer]]; [GTMHTTPFetcher setLoggingEnabled:YES]; [fetcher beginFetchWithCompletionHandler:^(NSData* data, NSError* error) { if (error) { NSLog(@"error => %@", [error localizedDescription]); } else { NSLog (@"You got the image");
     }];
}}
I enable the logging on Fetcher and the result is;
Request: GET https://www.google.com/m8/feeds/photos/media/example%40gmail.com/6338707b0cc6a57d
Request headers:
  Accept: image/*
  Authorization: Bearer _snip_
  GData-Version: 3.0

Response: status 401
Response headers:
  Cache-Control: private, max-age=0
  Content-Type: text/html; charset=utf-8
  Date: Thu, 29 Nov 2012 04:32:33 GMT
  Expires: Thu, 29 Nov 2012 04:32:33 GMT
  Server: GSE
  Transfer-Encoding: Identity
  X-Content-Type-Options: nosniff
  X-Frame-Options: SAMEORIGIN
  X-XSS-Protection: 1; mode=block

Any help appreciated.

Greg Robbins

unread,
Nov 29, 2012, 3:35:16 AM11/29/12
to gdata-objec...@googlegroups.com
Be sure the authorization is obtained with an appropriate scope to include fetching the photos, and also check the client API service settings in the API console, https://code.google.com/apis/console

prathumca

unread,
Nov 29, 2012, 3:45:43 AM11/29/12
to gdata-objec...@googlegroups.com
Greg, thanks for the reply.

Please see the scope I'm using.

NSString* scope = [NSString stringWithFormat:@"%@ %@", kGTLAuthScopePlusMe, [GDataServiceGoogleContact authorizationScope]];

GTMOAuth2ViewControllerTouch *viewController = [[GTMOAuth2ViewControllerTouch alloc] initWithScope:scope

                                                                                                  clientID:[MyAppDelegate apiClientID]

                                                                                               clientSecret:nil

                                                                                           keychainItemName:kKeychainItemName

                                                                                                   delegate:self

                                                                                           finishedSelector:@selector(viewController:finishedWithAuth:error:)];

I was able to fetch contact entries and its properties except images. I checked with api console and found that Google+ API is enabled for my app. Am missing anything else?

Thanks.

Greg Robbins

unread,
Nov 29, 2012, 5:43:44 PM11/29/12
to gdata-objec...@googlegroups.com
I would expect the fetch of a contact photo to work with the normal OAuth 2 authorization, and the authorization header appears to be present as expected in the http log shown earlier in this thread. (I assume you changed the email address to "exa...@gmail.com".)

So long as the OAuth 2 authorization was done with the appropriate scopes, the fetch should work. I don't know if any additional scopes are needed for the contacts API photos.

Reply all
Reply to author
Forward
0 new messages