2016-07-26 16 views
0

Ich habe den Code von this sample. Um die Anzahl der Benutzer-ungelesene Nachrichten zu erhalten (das ist, was ich brauche), muss ich diese GET Anfrage senden wieErhalten Sie Zugriff Token für Gmail API über Objective-C

https://www.googleapis.com/gmail/v1/users/me/labels/UNREAD?key={MY_API_KEY} 

in this example. Aber ich denke, dass die {ACCESS_TOKEN} hier statt {MY_API_KEY} sein sollte. Wenn ja, kann mir jemand sagen, wie man das Access Token mit AFNetworking oder auth from the sample bekommt?

Antwort

0

das Zugriffstoken Um eine authorise Anfrage an den Google-API, damit Sie die folgenden Methoden implementieren sollten:

- (GTMOAuth2ViewControllerTouch *)createAuthController { 
GTMOAuth2ViewControllerTouch *authController; 
// If modifying these scopes, delete your previously saved credentials by 
// resetting the iOS simulator or uninstall the app. 
NSArray *scopes = [NSArray arrayWithObjects:kGTLAuthScopeGmailReadonly, nil]; 
authController = [[GTMOAuth2ViewControllerTouch alloc] 
        initWithScope:[scopes componentsJoinedByString:@" "] 
        clientID:kClientID 
        clientSecret:nil 
        keychainItemName:kKeychainItemName 
        delegate:self 
        finishedSelector:@selector(viewController:finishedWithAuth:error:)]; 
return authController; 
} 


- (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController 
    finishedWithAuth:(GTMOAuth2Authentication *)authResult 
      error:(NSError *)error { 
if (error != nil) { 
    ... 
} 
else { 
    NSLog(@"Access token: %@", authResult.accessToken); 

} 
} 

Und Ihre ViewDidAppear Methode sollte wie folgt aussieht:

- (void)viewDidAppear:(BOOL)animated { 
if (!self.service.authorizer.canAuthorize) { 
    // Not yet authorized, request authorization by pushing the login UI onto the UI stack. 
    [self presentViewController:[self createAuthController] animated:YES completion:nil]; 
} 

Dieser Code gibt das Zielzugriffstoken aus.

0

diskutiert Wie in Authorizing Your App with Gmail

Gmail uses the OAuth 2.0 protocol for authenticating a Google account and authorizing access to user data. You can also use Google+ Sign-in to provide a "sign-in with Google" authentication method for your app.

Wenn AFNetworking mit noch Ihre Präferenz ist wie gewünscht, können Sie mit dem Guide verwenden, wie das Zugriffstoken in dieser GitHub Post gegeben bekommen - AFOAuth2Manager.

Lösung gegeben in diesem SO Post - How to get the number of unread threads in INBOX with Gmail API könnte auch helfen.

+0

Nach dem Beispiel habe ich Google-Anmeldung für Benutzer, aber nach der Autorisierungsanfrage noch 'https://www.googleapis.com/gmail/v1/users/me/labels/UNREAD?key= {MY_API_KEY}' gibt mir ein Fehler: Anfrage fehlgeschlagen: nicht autorisiert (401) – ArtStyle