簡體   English   中英

Google Contact API-401客戶端錯誤

[英]Google Contact API - 401 Client Error

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

}

嘗試這樣,但是首先您需要從GoogleOAuth獲取Accesstoken ,然后嘗試。

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

我希望這段代碼對您有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM