2012-11-01 51 views
4

我註冊我的谷歌API控制檯的應用程序。我得到我的客戶端祕密,客戶端ID和兩個重定向uris我應該使用什麼重定向uri(OAuth 2.0)?

//● 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]; 
} 

回答

2

您應該首先了解oAuth。

通常,第一個鏈接是授權流程 - 您可以調用它並獲取代碼。 第二個URL是使用您從前一個URL獲得的代碼獲取令牌。

解釋如何使用oAuth不在這裏,但你有許多地方可以閱讀和學習。

+0

謝謝你的回答。我正在學習OAuth,但仍只是初學者。但是......我已經對OAuth有所瞭解,並且有一個很大的問題。 如果在方便的時候回答我的問題,你介意嗎? – user1702650

+0

當我的應用程序將用戶重定向到授權頁面時,我必須發佈「客戶端機密」「範圍」和「重定向uri以便用戶授權我的應用程序後使用」 我選擇@「xxxxoob」作爲重定向uri你說,我認爲這是授權流程工作時需要的。)但是我仍然有錯誤「com.google.HTTPStatus錯誤404」 除了我使用錯誤的uri以外,可能嗎? – user1702650

+0

如果你得到404,我相信問題是授權頁面的URL,試着重新檢查一下,確保它沒問題,你可以打開一個專用於這個問題的新線程,並粘貼更多信息(比如你試圖撥打的URL等)。我的第一個答案已經幫助,隨時待客,並將其標記爲正確的(右下「0」):-) – OhadR

相關問題