在iPhone应用中使用Google Reader API和OAuth

时间:2010-09-22 05:50:59

标签: iphone objective-c oauth google-reader

我想制作一个在Google阅读器API上使用的应用。但是我发现它没有正式的API - 在App Store指南/批准方面使用非官方API是否存在问题?其他应用程序(Reeder等)会使用它吗?

登录的最佳方法是什么? OAuth是优先方法吗?使用Janrain是个好主意吗?

2 个答案:

答案 0 :(得分:7)

坦率地说,Apple并不关心您是否使用Google的非官方API。

我在使用Google阅读器进行同步的RSS阅读器应用程序上为客户工作。我们没有使用OAuth,而是使用标准的HTTP登录信息,它会返回一个cookie,您必须从中提取令牌,以便连续调用各种读者URL。

我可以从我的(旧)概念验证应用程序发布登录代码。 它使用ASIHTTP和一些自定义字符串类别。我们的想法是发送登录请求,获取响应并从响应的cookie头中提取会话ID /身份验证代码。然后,您可以将该会话ID /身份验证代码用于连续呼叫。

#pragma mark -
#pragma mark login

//this is your sessionID token you get from the login
//use this in consecutive calls to google reader
//this method returns you the header string you have to add to your request
//[request addRequestHeader: @"Cookie" value: [self sidHeader]];
- (NSString *) sidHeader
{
    return [NSString stringWithFormat: @"SID=%@", [self sid]];
}

- (NSString *) authHeader
{
    return [NSString stringWithFormat: @"GoogleLogin auth=%@",[self auth]];
}


//login to your google account and get the session ID
- (void) login
{
    NSString *username = @"my.googlelogin@gmail.com";
    NSString *password = @"mypassword123";
    NSString *loginUrl = @"https://www.google.com/accounts/ClientLogin?client=NNW-Mac";
    NSString *source = @"NNW-Mac"; //let's fake NetNewsWire
    NSString *continueUrl = @"http://www.google.com";

    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString: loginUrl]]; // log in & get cookies
    [request addRequestHeader: @"User-Agent" value: @"NetNewsWire/3.2b25 (Mac OS X; http://www.newsgator.com/Individuals/NetNewsWire/)"];

    [request setPostValue: username forKey: @"Email"];
    [request setPostValue: password forKey: @"Passwd"];
    [request setPostValue: @"reader" forKey: @"service"];
    [request setPostValue: source forKey: @"source"];
    [request setPostValue: continueUrl forKey: @"continue"];

    [request setDelegate: self];
    [request setDidFailSelector: @selector(loginRequestFailed:)];
    [request setDidFinishSelector: @selector(loginRequestFinished:)];

    [request start];
}   

-(void)loginRequestFinished:(ASIHTTPRequest *)request
{
    NSString *responseString = [request responseString];

    //login failed
    if ([responseString containsString: @"Error=BadAuthentication" ignoringCase: YES])
    {
        [self setLastError: [self errorWithDescription: @"Bad Username/Passsword" code: 0x001 andErrorLevel: 0x00]];

        if ([delegate respondsToSelector: @selector(gReaderLoginDidFail:)])
        {
            [delegate gReaderLoginDidFail: self];
        }

        return NO;
    }

    //captcha required
    if ([responseString containsString: @"CaptchaRequired" ignoringCase: YES])
    {
        [self setLastError: [self errorWithDescription: @"Captcha Required" code: 0x001 andErrorLevel: 0x00]];

        if ([delegate respondsToSelector: @selector(gReaderLoginDidFail:)])
        {
            [delegate gReaderLoginDidFail: self];
        }

        return NO;
    }

    //extract SID + auth
    NSArray *respArray = [responseString componentsSeparatedByCharactersInSet: [NSCharacterSet newlineCharacterSet]];

    NSString *sidString = [respArray objectAtIndex: 0];
    sidString = [sidString stringByReplacingOccurrencesOfString: @"SID=" withString: @""];
    [self setSid: sidString];

NSString *authString = [respArray objectAtIndex: 2];
authString = [authString stringByReplacingOccurrencesOfString: @"Auth=" withString: @""];
[self setAuth: authString];
    //mesage delegate of success
    if ([delegate respondsToSelector: @selector(gReaderLoginDidSucceed:)])
    {
        [delegate gReaderLoginDidSucceed: self];
    }

    return YES;
}

- (void)loginRequestFailed:(ASIHTTPRequest *)request
{
    NSError *error = [request error];

    //NSLog(@"login request failed with error: %@", [error localizedDescription]);
    [self setLastError: error];

    if ([delegate respondsToSelector: @selector(gReaderLoginDidFail:)])
    {
        [delegate gReaderLoginDidFail: self];
    }

}

登录后,您可以使用sid和auth伪造对Reader的API端点的请求。

示例:

- (ASIHTTPRequest *) requestForAPIEndpoint: (NSString *) apiEndpoint
{
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString: apiEndpoint]];
    [request addRequestHeader: @"User-Agent" value: @"NetNewsWire/3.2b25 (Mac OS X; http://www.newsgator.com/Individuals/NetNewsWire/)"];
    [request addRequestHeader: @"Cookie" value: [self sidHeader]];
    [request addRequestHeader: @"Authorization" value: [self authHeader]];

    return request;
}

关于Google阅读器及其私有API的有趣读物是http://timbroder.com/2007/08/google-reader-api-functions.html

请务必阅读最新评论:)

/ edit:我更新了代码以使用auth标头(google今年6月推出)。如果您使用OAuth,我想这将是您放置OAuth令牌的地方。 猜测

答案 1 :(得分:6)

我发现这一点:“Google Data API Objective-C Client Library提供了一个iPhone静态库,一个Mac OS X框架和源代码,可以通过Google Data API轻松访问数据。”{{3} } - 太棒了! 但它不包括Reader API(因为它尚未发布)。

我可以通过更改(在OAuthSampleTouch示例中)

来访问API
NSString *scope = @"http://www.google.com/m8/feeds/";

在OAuthSampleRootViewControllerTouch.m中

NSString *scope = @"http://www.google.com/reader/api/*";

urlStr = @"http://www.google.com/m8/feeds/contacts/default/thin";

urlStr = @"http://www.google.com/reader/atom/user/-/label/Design";

其中Design是文件夹名称 - 请查看code.google.com/p/gdata-objectivec-client这是一个很好的帮助。


<强>更新

我发现这种技术是最好的/最轻的/不那么复杂的: http://code.google.com/p/pyrfeed/wiki/GoogleReaderAPI