2011-01-18 1 views
3

google reader's API의 데이터를 사용하고 로그인 할 때 GData을 사용하는 앱을 개발 중입니다.iphone google reader 앱에서 읽음/읽지 않음으로 표시하기

테이블 셀 안의 게시물을 읽음/읽지 않음으로 표시 할 수 있기를 원하지만 실제로 솔루션/아이디어/아이디어를 찾기가 어렵다는 사실을 발견했습니다.

//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]); 

} 

이의 로그는 다음과 같습니다 : 여기

마크에 대한 코드는 버튼을 읽는다

당신이 PHP를 잘 알고있는 나는 구글 readi API를 사용하는 방법에 대한 자습서를 썼다면
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 
+0

저도 같은 문제를했습니다 그리고 난 당신의 코드를 시도 wan't하지만 난 authString 무엇인지 이해가 안 나는 그것을 얻을 수있는 방법! –

+0

안녕하세요, 저는 Auth 세부 정보를 얻는 방법을 포함하도록 답변을 업데이트했습니다. 도움이 되길 바랍니다. – daidai

+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]; 
0

요새. 내가 알 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=%@

귀하는 "편집"과 같아야 URL을 토큰을 포함하지 않는 것입니다. 당신은? 편집 태그 후.

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

목표 - C 물건에 도움이 수 없습니다 죄송합니다.

+0

고맙지 만 GData 로그인을 사용하여 헤더를 추가하거나 데이터를 게시해야한다고 생각합니다. – daidai

+0

google 리더와의 모든 상호 작용을 위해 헤더의 일부로 gdata 로그인이 필요합니다. 데이터를 추가, 제거 또는 업데이트하는 모든 상호 작용에 필요한 추가 토큰이 있습니다. – smilbandit

+0

예,하지만 [mAuth authorizeRequest : readerRequest]; 헤더에 OAoth 토큰을 추가해야하지만 나던 한 것 같습니다. – daidai

관련 문제