Google Contact API - 401客户端错误

时间:2015-04-13 04:26:34

标签: ios objective-c google-plus google-contacts

我在Google Contact API上遇到了问题。我正在使用关注代码从Google帐户中获取所有合同。

但它总是抛出401错误。有人可以帮我吗?

谢谢。

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

    return;
}


if(!error) {

    auth.clientID  =  @"Cient ID";
    auth.clientSecret  = @"Client Secret";
    auth.scope= @"https://www.googleapis.com/auth/contacts.readonly";

    NSString *urlStr = @"https://www.google.com/m8/feeds/contacts/default/full?v=3.0&alt=json&max-results=500";

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

}

1 个答案:

答案 0 :(得分:0)

尝试这样但首先您需要从Accesstoken获取GoogleOAuth然后尝试。

NSString *urlstring=[NSString stringWithFormat:@"https://www.google.com/m8/feeds/contacts/default/full?max-results=10000&access_token=%@",[UserDefaults objectForKey:@"AccessToken"]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlstring]];
[request setHTTPMethod:@"GET"];
[request setValue:@"3.0" forHTTPHeaderField:@"GData-Version"];
[request setValue:@"application/atom+xml" forHTTPHeaderField:@"Content-Type"];
NSHTTPURLResponse *returnresponse = nil;
NSData *GETReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&returnresponse error:nil];
NSString *theReply = [[NSString alloc] initWithBytes:[GETReply bytes] length:[GETReply length] encoding: NSUTF8StringEncoding];
NSData *jsonData = [theReply dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dict11=[XMLReader dictionaryForXMLData:jsonData error:nil];

在回复中,您将获得XML数据。所以你要解析XML

我希望这段代码对你有用。