2014-11-12 14 views
6

Nella mia applicazione abbiamo mantenuto l'opzione per accedere tramite Gmail. Ho bisogno di recuperare i contatti di Gmail.Come recuperare i contatti di Gmail nell'applicazione iOS utilizzando google contacts api?

Nella seguente metodo che sto usando oggetto auth (una volta il successo) per andare a prendere i contatti di Gmail con la creazione di richiesta con url: "https://www.google.com/m8/feeds/contacts/default/full"

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

auth.clientID =myClientId; 
auth.clientSecret =myClientSecret; 
auth.scope= @"https://www.googleapis.com/auth/contacts.readonly"; 

NSString *urlStr = @"https://www.google.com/m8/feeds/contacts/default/full"; 

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

sto ottenendo l'errore del cliente (401). c'è qualche cosa che mi manca alla mia richiesta.

+0

Controllare questa risposta: http://stackoverflow.com/a/23091506/1604312 –

+0

@ Per favore, aggiorna la tua risposta in modo che gli altri possano beneficiare della tua risposta. Ho una query simile e sto affrontando lo stesso problema .. grazie – ChenSmile

+0

Ciao @CKT se hai risolto questo. Quindi si prega di condividere le vostre soluzioni o suggerire i passaggi da fare. Grazie. –

risposta

0

la correttezza Scope è "https://www.google.com/m8/feeds"

a Swift

class func getContactsFromUser() { 
     let urlStr = "https://www.google.com/m8/feeds/contacts/default/full" 
     let url = NSURL(string: urlStr); 

     var request = NSMutableURLRequest(URL: url!) 

     let appd = UIApplication.sharedApplication().delegate as! AppDelegate 
     let error: NSError! 
     appd.service.authorizer.authorizeRequest!(request, completionHandler: { (error) -> Void in 

      if error != nil { 
       println("error getting contacts is \(error.localizedDescription)") 
      } else { 
       let response: AutoreleasingUnsafeMutablePointer<NSURLResponse?>=nil 

       let data = NSURLConnection.sendSynchronousRequest(request, returningResponse: response, error: nil) 

       if data != nil { 
        let stringResponse = NSString(data: data!, encoding: NSUTF8StringEncoding) 
        println("**** stringResponse **** \(stringResponse!)") 
       } else { 
        println("error 2 getting contacts is ") 
       } 
      } 
     }) 
    } 

in Objective C

- (void)doAnAuthenticatedAPIFetch { 
NSString *urlStr = @"https://www.google.com/m8/feeds/contacts/default/full"; 
NSURL *url = [NSURL URLWithString:urlStr]; 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
[self.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]; 
        } else { 
         // fetch failed 
         output = [error description]; 
        } 
       } 
      }]; 
} 
Problemi correlati