2010-09-27 29 views
3

我目前正在建立一個類似於成功的應用程序「Reeder for iPhone」的本地谷歌閱讀器iPhone應用程序,但是,還有一個完整的Twitter客戶端內置。原生谷歌閱讀器iPhone應用程序

我已經完成Twitter客戶端,現在正在努力啓動Google閱讀器客戶端。我已瀏覽了多個文檔,並看了一下gdata-objective-client樣本,但我似乎還不明白爲了完成與Reeder相同的功能,我必須做些什麼。

基本上我想能夠向用戶顯示登錄屏幕。然後用戶提交他們的憑證和訪問令牌,所有這些都是在場景後面完成的,就像他們使用Twitter的xAuth一樣。然後我想推送一個視圖控制器,顯示一個UITableView與所有當前未讀的提要。當用戶點擊UITableViewCell時,分別推送包含帖子內容的詳細視圖。

這是可能的,如果是這樣,我該如何去實現這些功能?如果有人發佈「代碼片段」並真正展示他們如何實現這些實現,我將不勝感激。

在此先感謝!

編輯:它已被引起我的注意,谷歌應用程序引擎是不需要的。然而,這個問題仍然是一樣的。我如何將Google Reader應用到我的應用程序中?

+0

爲什麼這個標籤爲google-app-engine? – Thilo 2010-09-27 08:06:22

+0

,因爲從我讀過它需要以某種方式實現谷歌應用程序引擎?除非你能夠幫助並指出我朝着正確的方向? – 2010-09-27 08:13:49

+0

App Engine是構建Web應用程序的基礎結構 - 它與純粹的客戶端iPhone應用程序無關。 – 2010-09-27 10:13:35

回答

9

這是如此簡單。對於所有想知道的,連接到谷歌閱讀器API,我做了以下。

/* Google clientLogin API: 
    Content-type: application/x-www-form-urlencoded 
    Email=userName 
    Passwd=password 
    accountType=HOSTED_OR_GOOGLE 
    service=xapi 
    source = @"myComp-myApp-1.0" 
    */ 

    //define our return objects 
    BOOL authOK; 
    NSString *authMessage = [[NSString alloc] init]; 
    NSArray *returnArray = nil; 
    //begin NSURLConnection prep: 
    NSMutableURLRequest *httpReq = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:GOOGLE_CLIENT_AUTH_URL] ]; 
    [httpReq setTimeoutInterval:30.0]; 
    //[httpReq setCachePolicy:NSURLRequestReloadIgnoringCacheData]; 
    [httpReq setHTTPMethod:@"POST"]; 
    //set headers 
    [httpReq addValue:@"Content-Type" forHTTPHeaderField:@"application/x-www-form-urlencoded"]; 
    //set post body 
    NSString *requestBody = [[NSString alloc] 
          initWithFormat:@"Email=%@&Passwd=%@&service=reader&accountType=HOSTED_OR_GOOGLE&source=%@", 
          gUserString, gPassString, [NSString stringWithFormat:@"%@%d", gSourceString]]; 

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

    NSHTTPURLResponse *response = nil; 
    NSError *error = nil; 
    NSData *data = nil; 
    NSString *responseStr = nil; 
    NSArray *responseLines = nil; 
    NSString *errorString; 
    //NSDictionary *dict; 
    int responseStatus = 0; 
    //this should be quick, and to keep same workflow, we'll do this sync. 
    //this should also get us by without messing with threads and run loops on Tiger. 
    data = [NSURLConnection sendSynchronousRequest:httpReq returningResponse:&response error:&error]; 

    if ([data length] > 0) { 
     responseStr = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; 
     //NSLog(@"Response From Google: %@", responseStr); 
     responseStatus = [response statusCode]; 
     //dict = [[NSDictionary alloc] initWithDictionary:[response allHeaderFields]]; 
     //if we got 200 authentication was successful 
     if (responseStatus == 200) { 
      authOK = TRUE; 
      authMessage = @"Successfully authenticated with Google. You can now start viewing your unread feeds."; 
     } 
     //403 = authentication failed. 
     else if (responseStatus == 403) { 
      authOK = FALSE; 
      //get Error code. 
      responseLines = [responseStr componentsSeparatedByString:@"\n"]; 
      //find the line with the error string: 
      int i; 
      for (i =0; i < [responseLines count]; i++) { 
       if ([[responseLines objectAtIndex:i] rangeOfString:@"Error="].length != 0) { 
        errorString = [responseLines objectAtIndex:i] ; 
       } 
      } 

      errorString = [errorString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
      /* 
      Official Google clientLogin Error Codes: 
      Error Code Description 
      BadAuthentication The login request used a username or password that is not recognized. 
      NotVerified The account email address has not been verified. The user will need to access their Google account directly to resolve the issue before logging in using a non-Google application. 
      TermsNotAgreed The user has not agreed to terms. The user will need to access their Google account directly to resolve the issue before logging in using a non-Google application. 
      CaptchaRequired A CAPTCHA is required. (A response with this error code will also contain an image URL and a CAPTCHA token.) 
      Unknown The error is unknown or unspecified; the request contained invalid input or was malformed. 
      AccountDeleted The user account has been deleted. 
      AccountDisabled The user account has been disabled. 
      ServiceDisabled The user's access to the specified service has been disabled. (The user account may still be valid.) 
      ServiceUnavailable The service is not available; try again later. 
      */ 

      if ([errorString rangeOfString:@"BadAuthentication" ].length != 0) { 
       authMessage = @"Please Check your Username and Password and try again."; 
      }else if ([errorString rangeOfString:@"NotVerified"].length != 0) { 
       authMessage = @"This account has not been verified. You will need to access your Google account directly to resolve this"; 
      }else if ([errorString rangeOfString:@"TermsNotAgreed" ].length != 0) { 
       authMessage = @"You have not agreed to Google terms of use. You will need to access your Google account directly to resolve this"; 
      }else if ([errorString rangeOfString:@"CaptchaRequired" ].length != 0) { 
       authMessage = @"Google is requiring a CAPTCHA response to continue. Please complete the CAPTCHA challenge in your browser, and try authenticating again"; 
       //NSString *captchaURL = [responseStr substringFromIndex: [responseStr rangeOfString:@"CaptchaURL="].length]; 
       //either open the standard URL in a browser, or show a custom sheet with the image and send it back... 
       //parse URL to append to GOOGLE_CAPTCHA_URL_PREFIX 
       //but for now... just launch the standard URL. 
       //[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:GOOGLE_CAPTCHA_STANDARD_UNLOCK_URL]];   
      }else if ([errorString rangeOfString:@"Unknown" ].length != 0) { 
       authMessage = @"An Unknow error has occurred; the request contained invalid input or was malformed."; 
      }else if ([errorString rangeOfString:@"AccountDeleted" ].length != 0) { 
       authMessage = @"This user account previously has been deleted."; 
      }else if ([errorString rangeOfString:@"AccountDisabled" ].length != 0) { 
       authMessage = @"This user account has been disabled."; 
      }else if ([errorString rangeOfString:@"ServiceDisabled" ].length != 0) { 
       authMessage = @"Your access to the specified service has been disabled. Please try again later."; 
      }else if ([errorString rangeOfString:@"ServiceUnavailable" ].length != 0) { 
       authMessage = @"The service is not available; please try again later."; 
      } 

     }//end 403 if 

    } 
    //check most likely: no internet connection error: 
    if (error != nil) { 
     authOK = FALSE; 
     if ([error domain] == NSURLErrorDomain) { 
      authMessage = @"Could not reach Google.com. Please check your Internet Connection"; 
     }else { 
      //other error 
      authMessage = [authMessage stringByAppendingFormat:@"Internal Error. Please contact notoptimal.net for further assistance. Error: %@", [error localizedDescription] ]; 
     } 
    } 
    //NSLog (@"err localized description %@", [error localizedDescription]) ; 
    //NSLog (@"err localized failure reasons %@", [error localizedFailureReason]) ; 
    //NSLog(@"err code %d", [error code]) ; 
    //NSLog (@"err domain %@", [error domain]) ; 


    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Authentication" message:authMessage delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil]; 
    [alertView show]; 
    [alertView release]; 

    [gUserString release]; 
    [gPassString release]; 
    [gSourceString release]; 

    [authMessage release]; 

} 

} 

顯然我使用我自己的代表等,但這是我帶到我的應用程序的整體想要/感覺。

我目前正致力於將未讀的提要/項目拖入UITableView以顯示在我的RootViewController中。我會用更多的信息更新這個。

感謝所有試圖提供幫助的人:D

+0

感謝這個解決方案,我一直拉着我的頭髮這幾周 – daidai 2011-01-24 04:16:31

0

谷歌它,有很多博客描述如何使用谷歌閱讀器API。這是一個體面的起點: http://mindsharestrategy.com/google-reader-api-a-brief-tutorial/

+0

他們解釋瞭如何在python中使用它,幾乎沒有任何關於在Objective-C中使用它的詳細信息... – 2010-09-27 08:18:32

+0

@ the0rkus - 該教程在PHP中...不是Python。但它應該是非常可轉讓的。 API使用標準的HTTP請求/響應對進行通信,所以您只需要知道如何進行GET/POST請求以及如何解析Objective-C中的響應。如果你不這樣做,那麼在嘗試與任何類型的外部API交互之前,你應該知道這一點。 – EAMann 2011-05-17 13:58:58

2

謝謝。我知道有一種簡單的登錄方式,但是我正在度過一段可怕的時間。順便說一句,對於那些複製/使用0rkus代碼的人 - 你會得到一些錯誤。爲了測試它,我添加了:

NSString *gUserString = @"[email protected]"; 
NSString *gPassString = @"yourpassword"; 
NSString *GOOGLE_CLIENT_AUTH_URL = @"https://www.google.com/accounts/ClientLogin?client=YourClient"; 
NSString *gSourceString = @"YourClient"; 
+0

從哪裏可以得到GOOGLE_CLIENT_AUTH_URL和gSourceString?我需要在谷歌網站上的某個地方註冊我的應用嗎? – Shri 2011-11-19 10:13:32