通过 objective-c 获取 gmail api 的访问令牌
Get access token for gmail api via objective-c
我使用了 this sample 中的代码。要获取用户未读消息的数量(这就是我需要的),我需要发送此 GET
请求
https://www.googleapis.com/gmail/v1/users/me/labels/UNREAD?key={MY_API_KEY}
喜欢 this example. But I guess that the {ACCESS_TOKEN}
should be here instead of {MY_API_KEY}
. If so, could anybody tell me how to get the access token using AFNetworking or auth from the sample?
如 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.
如果您仍然喜欢按要求使用 AFNetworking,您可以使用此 GitHub post - AFOAuth2Manager.[=14 中有关如何获取访问令牌的指南=]
此 SO post 中给出的解决方案 - How to get the number of unread threads in INBOX with Gmail API 也可能有帮助。
要获取访问令牌以向 Google API 发出授权请求,您应该实施以下方法:
- (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);
}
}
您的 ViewDidAppear 方法应如下所示:
- (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];
}
该代码输出目标访问令牌。
我使用了 this sample 中的代码。要获取用户未读消息的数量(这就是我需要的),我需要发送此 GET
请求
https://www.googleapis.com/gmail/v1/users/me/labels/UNREAD?key={MY_API_KEY}
喜欢 this example. But I guess that the {ACCESS_TOKEN}
should be here instead of {MY_API_KEY}
. If so, could anybody tell me how to get the access token using AFNetworking or auth from the sample?
如 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.
如果您仍然喜欢按要求使用 AFNetworking,您可以使用此 GitHub post - AFOAuth2Manager.[=14 中有关如何获取访问令牌的指南=]
此 SO post 中给出的解决方案 - How to get the number of unread threads in INBOX with Gmail API 也可能有帮助。
要获取访问令牌以向 Google API 发出授权请求,您应该实施以下方法:
- (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);
}
}
您的 ViewDidAppear 方法应如下所示:
- (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];
}
该代码输出目标访问令牌。