在iphone谷歌阅读器应用程序中将项目标记为已读/未读

时间:2011-01-18 03:32:29

标签: iphone objective-c oauth google-reader

我正在使用google reader's API中的数据并使用GData进行登录来开发应用。

我希望能够将表格单元格中的帖子标记为已读/未读,但我发现这一切都是主要没有文档记录,很难找到解决方案,任何想法/链接?

以下是标记读取按钮的代码:

//Get the token

NSString *tokenString = [NSString stringWithFormat:@"https://www.google.com/reader/api/0/token"];

NSURL *tokenURL = [NSURL URLWithString:tokenString];
NSMutableURLRequest *tokenRequest = [NSMutableURLRequest requestWithURL:tokenURL];

NSString *tokenStringResult;
NSArray *listItems;

NSError *tokenError = nil;
NSURLResponse *tokenResponse = nil;
NSData *tokenData = [NSURLConnection sendSynchronousRequest:tokenRequest
                                          returningResponse:&tokenResponse
                                                      error:&tokenError];
if (tokenData) 
{
    tokenStringResult = [[NSString alloc] initWithData:tokenData encoding:NSUTF8StringEncoding];

    listItems = [tokenStringResult componentsSeparatedByString:@"/"];
} 
else 
{
    NSLog(@"tokenError = %@", tokenError);
}

// Mark it as read

NSString *readerURLString = [NSString stringWithFormat:@"http://www.google.com/reader/api/0/edit-tag?a=user/-/state/com.google/read&async=true&s=feed/%@&i=%@&T=%@", siteLink, postID, [listItems lastObject]];

NSURL *readerURL = [NSURL URLWithString:readerURLString];

NSMutableURLRequest *readerRequest = [NSMutableURLRequest requestWithURL:readerURL];

[mAuth authorizeRequest:readerRequest];

NSError *error = nil;
NSURLResponse *response = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:readerRequest
                                     returningResponse:&response
                                                 error:&error];
if (data) 
{
    NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse *) response;
    assert( [httpResponse isKindOfClass:[NSHTTPURLResponse class]] );

    NSLog(@"response.allHeaderFields = %@", [httpResponse allHeaderFields]);

    NSLog(@"response.statusCode = %i", [httpResponse statusCode]);

} 

这个日志是:

response.statusCode = {
"Cache-Control" = "private, max-age=0";
"Content-Length" = 1334;
"Content-Type" = "text/html; charset=UTF-8";
Date = "Fri, 21 Jan 2011 03:49:07 GMT";
Expires = "Fri, 21 Jan 2011 03:49:07 GMT";
Server = GSE;
"X-Content-Type-Options" = nosniff;
"X-Frame-Options" = SAMEORIGIN;
"X-Reader-Google-Version" = "527-000";
"X-Reader-User" = 01940378872835844713;
"X-Xss-Protection" = "1; mode=block";
}
response.statusCode = 400

2 个答案:

答案 0 :(得分:1)

所以通过大量的反复尝试,我得到了它。

编辑 - 添加了验证码

NSString *GOOGLE_CLIENT_AUTH_URL    = @"https://www.google.com/accounts/ClientLogin?client=SomeName";
NSString *gSourceString             = @"SomeName";

NSMutableURLRequest *httpReq = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:GOOGLE_CLIENT_AUTH_URL]];
[httpReq setTimeoutInterval:30.0];
[httpReq setCachePolicy:NSURLRequestReloadIgnoringCacheData];
[httpReq setHTTPMethod:@"POST"];

[httpReq addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

NSString *requestBody = [[NSString alloc]  initWithFormat:@"Email=%@&Passwd=%@&service=reader&accountType=HOSTED_OR_GOOGLE&source=%@", userString, passwordString, [NSString stringWithFormat:@"%@%d", gSourceString]];

[httpReq setHTTPBody:[requestBody dataUsingEncoding:NSASCIIStringEncoding]];

[requestBody release];

NSHTTPURLResponse *response = nil;

NSData *data    = nil;
NSString *responseStr       = nil;
NSArray *responseLines      = nil;

int responseStatus = 0;

data = [NSURLConnection sendSynchronousRequest:httpReq returningResponse:&response error:&error];

[httpReq release];

if ([data length] > 0) 
{
    responseStr = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
    responseStatus = [response statusCode];

    if (responseStatus == 200 ) 
    {
        authOK = TRUE;
        NSLog(@"Successfully authenticated with Google.");
        NSArray *authLines = nil;
        authLines  = [responseStr componentsSeparatedByString:@"\n"];

        int j;
        for (j =0; j < [authLines count]; j++ ) 
        {
            if ([[authLines objectAtIndex:j] rangeOfString:@"Auth="].length != 0) {

                NSMutableString *teststring = [NSMutableString stringWithString:[authLines objectAtIndex:j]];

                [teststring replaceCharactersInRange:NSMakeRange(0,4) withString:@"auth"];

                authString = teststring;
            }
        }
    }
    }


 NSString *auth = [[NSString alloc] initWithString: [NSString stringWithFormat:@"GoogleLogin %@", authString]];

NSDictionary *createHeader = [[NSDictionary dictionaryWithObjectsAndKeys:@"www.google.com", @"Host", @"EditApp", @"User-Agent", @"gzip, deflate", @"Accept-Encoding", auth, @"Authorization", nil]retain];

[auth release];

NSURL *url =[NSURL URLWithString:@"http://www.google.com/reader/api/0/token?client=EditApp"];
NSData *recieveData;
NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc]initWithURL:url];
[urlRequest setHTTPMethod:@"GET"];
[urlRequest setAllHTTPHeaderFields:createHeader];
NSURLResponse *response;
NSError *error;
recieveData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];

// Get token 

NSString *pretokenString = [[NSString alloc] initWithData:recieveData encoding:NSASCIIStringEncoding];

tokenString = [pretokenString substringWithRange:NSMakeRange(2, [pretokenString length]-2)];

[pretokenString release];

[urlRequest release];

NSMutableURLRequest *thttpReq = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.google.com/reader/api/0/edit-tag?"]];

[thttpReq setTimeoutInterval:30.0];

[thttpReq setHTTPMethod:@"POST"];

NSString *authHeader = [NSString stringWithFormat:@"GoogleLogin %@", authString];
[thttpReq addValue:authHeader forHTTPHeaderField:@"Authorization"];

[thttpReq addValue:@"Content-Type" forHTTPHeaderField:@"application/x-www-form-urlencoded"];

// siteLink is the url of the feed
// googlePostID is the id from the XML output: tag:google.com,2005:reader/item/e3345c69e174bdec

NSString *trequestBody = [[NSString alloc] initWithFormat:@"a=user/-/state/com.google/read&ac=edit-tags&s=feed/%@&i=%@&T=%@", siteLink, googlePostID, tokenString];

[thttpReq setHTTPBody:[trequestBody dataUsingEncoding:NSASCIIStringEncoding]];

NSURLConnection *con = [[NSURLConnection alloc]
                            initWithRequest:thttpReq
                            delegate:self
                            startImmediately:NO];
[con scheduleInRunLoop:[NSRunLoop currentRunLoop]
                   forMode:NSRunLoopCommonModes];
[con start];

答案 1 :(得分:0)

如果你熟悉php,我最近写了一篇关于使用google readi api的教程。 http://mobile.tutsplus.com/tutorials/mobile-web-apps/building-a-mobile-web-application-with-the-google-reader-api/

我注意到的一件事就是在这个字符串中

http://www.google.com/reader/api/0/edit-tag/a=user/-/state/com.google/read&async=true&s=feed/%@&i=%@

您不包括“编辑”令牌,网址应如下所示。你需要吗?在编辑标签之后。

http://www.google.com/reader/api/0/edit-tag?a=user/-/state/com.google/read&async=true&s=feed/%@&i=%@&T=%@

用户登录后,您可以从此链接获取“编辑”令牌https://www.google.com/reader/api/0/token

抱歉无法帮助解决客观问题。