如何从Gmail联系人获取朋友个人资料图片?

时间:2013-04-01 06:07:47

标签: ios

我正在尝试导入GMail联系人(朋友列表)。我有朋友的邮件ID,姓名和电话号码,但如果我试图获取图片,则显示“401错误”。

我使用的代码如下:

GDataLink *photoLink = [contact photoLink];
NSLog(@"%@",photoLink);

NSURL *imageURL = [photoLink URL];
NSLog(@"image url = %@",imageURL);

如何获取朋友的头像?我哪里出错了?

1 个答案:

答案 0 :(得分:1)

Looking at some other (public) code that's roughly the same as what you're doing,我发现您应该使用这些照片链接使用 身份验证令牌 。如果没有该身份验证令牌,Google会认为您可能是恶意的,并会发回401错误。

E.G。

    GDataLink *photoLink = [contact photoLink];

    NSString *imageETag = [photoLink ETag];
    if (imageETag == nil || ![mContactImageETag isEqual:imageETag]) {

      if (imageETag != nil) {

        // get an NSURLRequest object with an auth token
        NSURL *imageURL = [photoLink URL];
        GDataServiceGoogleContact *service = [self contactService];

        // requestForURL:ETag:httpMethod: sets the user agent header of the
        // request and, when using ClientLogin, adds the authorization header
        NSMutableURLRequest *request = [service requestForURL:imageURL
                                                         ETag:nil
                                                   httpMethod:nil];

        [request setValue:@"image/*" forHTTPHeaderField:@"Accept"];

        GTMHTTPFetcher *fetcher = [GTMHTTPFetcher fetcherWithRequest:request];
        [fetcher setAuthorizer:[service authorizer]];
        [fetcher beginFetchWithDelegate:self
                 didFinishSelector:@selector(imageFetcher:finishedWithData:error:)];
      }
    }
  }