1

私のアプリケーションでは、Googleアカウントへのアクセスを実装しようとしましたが、初期化するとログインセッションまで機能します。その後、スクリーンショットで次のエラーがスローされます

ここに画像の説明を入力ここに画像の説明を入力

ここで私のコードの 初期化とメソッドの実装

 static NSString *const kKeychainItemName =nil;
 NSString *kMyClientID = @"465568347336.apps.googleusercontent.com";     
 NSString *kMyClientSecret = @"rKVsWXTlo3M8zqNfofkX0Xrl"; 
 NSString *scope = @"https://www.googleapis.com/auth/userinfo.profile"; 

 GTMOAuth2ViewControllerTouch *viewController;
 viewController = [[GTMOAuth2ViewControllerTouch alloc] initWithScope:scope
                                                            clientID:kMyClientID
                                                        clientSecret:kMyClientSecret
                                                    keychainItemName:kKeychainItemName
                                                            delegate:self finishedSelector:@selector(viewController:finishedWithAuth:error:)];
 [self.navigationController presentModalViewController:viewController animated:YES];

エラーハンドラ

 - (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController
  finishedWithAuth:(GTMOAuth2Authentication *)auth
             error:(NSError *)error {
 if (error != nil) {
    NSString *output=nil;
    output = [error description];
    NSLog(@"output:%@",output);
    UIAlertView *fail = [[UIAlertView alloc] initWithTitle:@"Alert"
                                                   message:[NSString stringWithFormat:@"Error, Authentication failed!\n %@",error]
                                                  delegate:self
                                         cancelButtonTitle:@"OK"
                                         otherButtonTitles:@"Try again", nil];
    fail.tag = 1;

    [fail show];
    NSLog(@"Authentication failed!");
  } else {
    UIAlertView *success = [[UIAlertView alloc] initWithTitle:@"Alert"
                                                      message:[NSString stringWithFormat:@"Authentication succeeded!"]
                                                     delegate:self
                                            cancelButtonTitle:@"OK"
                                            otherButtonTitles:nil];
    success.tag = 2;

    [success show];
    NSLog(@"Autzentication succeeded!");
   }

これを解決する方法は問題です。解決するのを手伝ってください

4

2 に答える 2

3

次のコードを使用して GTMOAuth2 を実装しましたが、うまくいきました。何らかの形で役立つことを願っています。

- (GTMOAuth2Authentication * )authForGoogle
{    
    NSURL * tokenURL = [NSURL URLWithString:GoogleTokenURL];

    NSString * redirectURI = @"urn:ietf:wg:oauth:2.0:oob";

    _auth = [GTMOAuth2Authentication authenticationWithServiceProvider:@"Google"
                                                         tokenURL:tokenURL
                                                      redirectURI:redirectURI
                                                         clientID:GoogleClientID
                                                     clientSecret:GoogleClientSecret];
    _auth.scope = @"https://www.googleapis.com/auth/userinfo.profile";
    return _auth;
}


- (void)signInToGoogle
{   
    _auth = [self authForGoogle];

    // Display the authentication view
    GTMOAuth2ViewControllerTouch * viewController = [[GTMOAuth2ViewControllerTouch alloc] initWithAuthentication:_auth
                                                                                                 authorizationURL:[NSURL URLWithString:GoogleAuthURL]
                                                                                            keychainItemName:@"GoogleKeychainName"
                                                                                                    delegate:self
                                                                                            finishedSelector:@selector(viewController:finishedWithAuth:error:)];
    [_window setRootViewController: viewController];
    [_window makeKeyAndVisible];
}

 - (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController finishedWithAuth:(GTMOAuth2Authentication *)auth
         error:(NSError *)error 
{
    if (error != nil) {
       NSString *output=nil;
       output = [error description];
       NSLog(@"output:%@",output);
       UIAlertView *fail = [[UIAlertView alloc] initWithTitle:@"Alert"
                                               message:[NSString stringWithFormat:@"Error, Authentication failed!\n %@",error]
                                              delegate:self
                                     cancelButtonTitle:@"OK"
                                     otherButtonTitles:@"Try again", nil];
       fail.tag = 1;

       [fail show];
       NSLog(@"Authentication failed!");
    } else {
       UIAlertView *success = [[UIAlertView alloc] initWithTitle:@"Alert"
                                                  message:[NSString stringWithFormat:@"Authentication succeeded!"]
                                                 delegate:self
                                        cancelButtonTitle:@"OK"
                                        otherButtonTitles:nil];
       success.tag = 2;

       [success show];
       NSLog(@"Autzentication succeeded!");
    }
}

私の実装に似ているので、あなたの finishedWithAuth メソッドをコピーしました。xcode での GTMOAuth2 の実装に関しては、コード間に大きな違いはないと思いますが、GTMOAuth2 を使用していて気づいたことの 1 つは、直面したエラーをデバッグするのが非常に難しいということでした。私も同様のエラーが発生しましたが、アプリをセットアップして clientID と clientSecret を取得するときに、Google ポータルで間違った種類のアプリを選択したことが原因であることに気付きました。最初は iOS アプリとして設定しましたが (もちろん!)、オンラインでさまざまな回答や問題を読んだ後、その他のアプリ タイプとして作成する必要があることに気付きました。それは私の問題を解決しました。多分あなたはそれをチェックすることができます。

彼らはサポートが非常に包括的なフォーラムを持っています。これはここのリンクです

さらに追加するために、GTMOAuth2 をアプリに統合するときに参照したチュートリアルを参照できます。これはここのリンク です

また、ユーザーのメールアドレスを確認する必要があるエンタープライズアプリを開発していたため、ユーザーが認証された後でもメールを取得することは非常に困難でした. 必要なものを入手する前に、コードをハックして読み取る必要がありました。将来必要になった場合は、SO で私の回答を確認してください。

お役に立てれば!:)

于 2013-10-29T03:19:57.570 に答える
0

Error Domain=com.google.GTMOAuth2 Code=-1001通常、ユーザーがログインした場合に発生しますが、OAuth 契約ウィンドウ (たとえば、「電子メールにアクセスしたい」と表示されている場所) で、ユーザーは「キャンセル」または「いいえ」をクリックします。

したがって、基本的にこれを「解決」する方法はありません。そのサービスがなくても、それを処理するか、アプリを進めることができます。

于 2016-02-05T14:16:34.007 に答える