2017-04-22 2 views
0

J'essaie d'obtenir un contact Google en utilisant Google contact api.Comment obtenir des contacts google de google contact api?

Je reçois toutes les authentifications de google mais je ne sais pas comment appeler google contact api. Je l'ai fait déjà:

NSLog(@"\n\n User Access Token : %@",user.authentication.accessToken); 
NSLog(@"\n\n User Refresh Token : %@",user.authentication.refreshToken); 
NSLog(@"\n\n Person Token Expire Date : %@",user.authentication.accessTokenExpirationDate); 
NSLog(@"\n\n Person Client ID : %@",user.authentication.clientID); 

Je suis en train de code suivant pour appeler le contact api mais obtenir une réponse inattendue:

Réponse: Fin prématurée du fichier.

NSString * urlString = [NSString stringWithFormat:@"https://www.google.com/m8/feeds/contacts/default/full/"]; 
    urlString =[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 

    NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] 
                  cachePolicy:NSURLRequestReloadIgnoringLocalCacheData 
                 timeoutInterval:60.0]; 
    //do post request for parameter passing 
    [theRequest setHTTPMethod:@"POST"]; 

    [theRequest setValue:@"application/atom+xml" forHTTPHeaderField:@"Content-Type"]; 

    [theRequest addValue:[NSString stringWithFormat:@"Bearer %@",user.authentication.accessToken] forHTTPHeaderField:@"Authorization"]; 
    [theRequest addValue:@"3.0" forHTTPHeaderField:@"GData-Version"]; 

    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 

ici je reçois le contact mais comment obtenir dans mon application.

Google OAuth 2.0 Playground

merci à l'avance.

Répondre

0

Sur la base de la documentation:

Voici les informations de portée OAuth 2.0 pour l'API Google Contacts:

- https://www.google.com/m8/feeds/ - un accès en lecture/écriture aux contacts et groupes de contacts

- https://www.googleapis.com/auth/contacts.readonly - accès en lecture seule aux contacts et groupes de contacts

ensuite suivre ces codes de certains liés SO question refrence - How to fetch gmail Contacts in iOS application using google contacts api? et Get Google contacts using API on iOS:

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 ") 
       } 
      } 
     }) 
    } 

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

Hope this helps.

+0

s'il vous plaît donnez-moi le code source parfait. Je ne peux pas trouver 'self.auth'. @ Mr.Rebot – Application

+0

Où puis-je trouver 'authorizeRequest'? @ Mr.Rebot – Application