0
У меня проблема с API контактов Google. Я использую следующий код для получения всех контрактов из учетной записи google.Google Contact API - 401 Ошибка клиента
Но он всегда выдает ошибку 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];
}
}
}];
}
}
Я использовал токен доступа как этот. NSString * str = auth.accessToken; NSString * urlStr = [NSString stringWithFormat: @ "https://www.google.com/m8/feeds/contacts/default/full?v=3.0&alt=json&max-results=500&access_token=%@", str]; , но он все равно возвращает тот же результат. – adeltahir
Просто добавьте эту строку в ВАШЕМ КОДЕ [запрос setValue: @ "application/atom + xml" forHTTPHeaderField: @ "Content-Type"]; а затем попробуйте пропустить эту строку в вашем коде –
@adeltahir И помните, что сначала вам нужно получить аутентификацию Google, после чего вы сможете получить контакты из Google. –