我为 google API Console 注册了我的应用程序。我得到了我的客户秘密、客户 ID,以及两个重定向 uri.
//● urn:xxxxxxx:oob
//● http://localhostxxxxxx
当然,我使用这些项目并成功向谷歌请求令牌。但是当我单击“授权”按钮时(例如“您想授权此应用程序吗?”是的),
发生两种反应。
如果我使用 urnxxxxxx,我会收到“操作无法完成。(com.google.HTTPStatus 错误 404。)”。
//Or If I use http://localhostxxxxxxxxxxxxx and click Yes button, then nothing
happens.
接下来我应该做什么? (以下代码适用于谷歌阅读器。)
#import "MasterViewController.h"
#import "DetailViewController.h"
#import "GTMOAuth2Authentication.h"
#import "GTMOAuth2ViewControllerTouch.h"
#import "GTMOAuth2WindowController.h"
static NSString *const kKeychainItemName = @"Greader";
@interface MasterViewController () {
NSMutableArray *_objects;
}
@end
@implementation MasterViewController
- (IBAction)authentication:signInToGoogle:(id)sender;
{}
- (GTMOAuth2Authentication * ) authForGoogle
{
NSString * url_string = @"http://www.google.com/reader/api/";
NSURL * tokenURL = [NSURL URLWithString:url_string];
NSString * redirectURI = @"xxxxoob";
GTMOAuth2Authentication * auth;
auth = [GTMOAuth2Authentication authenticationWithServiceProvider:@"reader"
tokenURL:tokenURL
redirectURI:redirectURI
clientID:@"xxxxx"
clientSecret:@"xxxx"];
auth.scope = @"http://www.google.com/reader/api/";
return auth;
}
- (void)signInToGoogle
{
GTMOAuth2Authentication * auth = [self authForGoogle];
NSString* auth_string = @"https://accounts.google.com/o/oauth2/auth";
NSURL * authURL = [NSURL URLWithString:auth_string];
GTMOAuth2ViewControllerTouch * viewController;
viewController = [[GTMOAuth2ViewControllerTouch alloc]initWithAuthentication:auth
authorizationURL:authURL
keychainItemName:kKeychainItemName
delegate:self
finishedSelector:@selector(viewController:finishedWithAuth:error:)];
[self.navigationController pushViewController:viewController animated:YES];
}
您应该首先了解 oAuth。
一般来说,第一个链接是授权流程 - 您调用它并获取代码。
第二个 URL 是使用从前一个 URL 获取的代码来获取令牌。
解释如何准确地使用 oAuth 超出了本文的范围,但是您可以在很多地方阅读和学习。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)