如何在使用Google OAuth 2.0时阻止显示访问代码的额外视图

Dun*_*Dun 11 ios oauth-2.0

我按照http://googlemac.blogspot.com/2011/05/ios-and-mac-sign-in-controllers.html跟踪,允许用户使用Google登录iPhone应用.点击"允许访问"按钮后,我得到一个额外的屏幕,上面写着"请复制此代码,切换到您的应用程序并将其粘贴到那里:(文本框中的代码)."

这就是我所拥有的:

- (IBAction)googleLoginTapped:(UIButton *)sender
{
    [self loginToGoogle];
}

- (void)loginToGoogle 
{

    // For Google APIs, the scope strings are available
    // in the service constant header files.
    NSString *scope =@"https://www.googleapis.com/auth/userinfo.profile";

    // Typically, applications will hardcode the client ID and client secret
    // strings into the source code; they should not be user-editable or visible.

    // But for this sample code, they are editable.
    NSString *clientID = @"my clientID";
    NSString *clientSecret = @"my clientSecret";


    // Display the autentication view.
    SEL finishedSel = @selector(viewController:finishedWithAuth:error:);

    GTMOAuth2ViewControllerTouch *viewController;

    viewController = [GTMOAuth2ViewControllerTouch controllerWithScope:scope
                                                              clientID:clientID
                                                          clientSecret:clientSecret
                                                      keychainItemName:nil
                                                              delegate:self
                                                      finishedSelector:finishedSel];

    // For this sample, we'll force English as the display language.
    NSDictionary *params = [NSDictionary dictionaryWithObject:@"en"
                                                       forKey:@"hl"];

    viewController.signIn.additionalAuthorizationParameters = params;

    // Optional: display some html briefly before the sign-in page loads
    NSString *html = @"<html><body bgcolor=silver><div align=center>Loading sign-in page...</div></body></html>";
    viewController.initialHTMLString = html;

    viewController.signIn.shouldFetchGoogleUserProfile = YES;

    [self presentModalViewController:viewController animated:YES];
}

- (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController finishedWithAuth:(GTMOAuth2Authentication *)auth error:(NSError *)error
{
    if (error != nil) 
    {
        // Authentication failed (perhaps the user denied 
Run Code Online (Sandbox Code Playgroud)

请看这个链接是好的 https://developers.google.com/accounts/docs/OAuth2InstalledApp

Fel*_*ile 9

我试过这种方法,并且工作正常,把它放在你的webViewDidFinishLoad方法中:

if ([webView.request.URL.absoluteString rangeOfString:@"https://accounts.google.com/o/oauth2/approval?"].location != NSNotFound) {
    webView.hidden=YES;
}
Run Code Online (Sandbox Code Playgroud)

  • 我做了同样的事,但无法隐藏webview.即使在我按照上述条件隐藏它之后,它仍然可见.我也确定断点. (2认同)

Dun*_*Dun 6

我找到了答案.最初我使用客户端ID来安装应用程序.这没有提供设置重定向URI的选项.它给出了一个默认的重定向URI:urn:ietf:wg:oauth:2.0:oob http:// localhost.所以当我使用此代码发送身份验证请求时:

viewController = [GTMOAuth2ViewControllerTouch controllerWithScope:scope
                                                              clientID:clientID
                                                          clientSecret:clientSecret
                                                      keychainItemName:nil
                                                              delegate:self
                                                      finishedSelector:finishedSel];
Run Code Online (Sandbox Code Playgroud)

它返回了一个成功代码,然后用于授权本机应用程序.有关返回的代码或令牌的更多信息,请在此处查看.

为了解决我的问题,我继续使用客户端ID进行Web应用程序.这允许我显式设置重定向URI,并允许我在这里将response_type设置为token而不是代码:

https://accounts.google.com/o/oauth2/auth?
  client_id=21302922996.apps.googleusercontent.com&
  redirect_uri=https://www.example.com/back&
  scope=https://www.google.com/m8/feeds/&
  response_type=**token**
Run Code Online (Sandbox Code Playgroud)

因此,当我通过身份验证并从服务器返回redirect_uri时,它会附加一个"access_tocken"作为查询字符串,如下所示:

https://www.example.com/back?access_token=returned_access_tocken
Run Code Online (Sandbox Code Playgroud)

现在您可以使用正则表达式代码:

-(void)checkForAccessToken:(NSString *)urlString {
    NSError *error;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"access_token=(.*)&" options:0 error:&error];
    if (regex != nil) 
    {
        NSTextCheckingResult *firstMatch = [regex firstMatchInString:urlString options:0 range:NSMakeRange(0, [urlString length])];
        if (firstMatch) 
        {
            NSRange accessTokenRange = [firstMatch rangeAtIndex:1];
            NSString *accessToken = [urlString substringWithRange:accessTokenRange];
            accessToken = [accessToken stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            [_delegate accessTokenFoundGoogle:accessToken];
            accessTokenFound = YES;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在这里选择access_code并将其用于您的授权请求:

"https://www.googleapis.com/oauth2/v1/userinfo?oauth_token=put_your_accesstoken_here" to send a request for authorization
Run Code Online (Sandbox Code Playgroud)

然后你可以发送你的请求,并在这种情况下以JSON格式返回用户的个人资料信息.您可以参考这个问题和答案,使用Facebook的图形API来登录用户.然后,只需使用我在此处提供的建议和请求网址修改代码即可使用新的Google OAuth 2.0.只是在转换Facebook代码时为您加速的建议,创建一个新的init方法,如下所示:

- (id)initWithDelegate:(id<GoogleLoginDialogDelegate>)delegate;

- (id)initWithDelegate:(id<GoogleLoginDialogDelegate>)delegate 
{
    if ((self = [super initWithNibName:@"GoogleLoginDialog" bundle:[NSBundle mainBundle]])) {
        self.delegate = delegate;
    }
    return self;    
}
Run Code Online (Sandbox Code Playgroud)

这样您就可以轻松使用Google登录对话框中的委托方法.如果您仔细关注Facebook示例,那么您应该让Google登录OAuth工作得非常好!


kev*_*lar 5

事实证明这非常简单.在登录回调中,只需viewController从父视图控制器中解除并删除.

- (void)viewController:(UIViewController *)viewController
      finishedWithAuth:(GTMOAuth2Authentication *)auth
                 error:(NSError *)error
{
    if (error == nil) {
        // Get rid of the login view.
        // self.parentViewController was saved somewhere else and is the parent
        // view controller of the view controller that shows the google login view.
        [self.parentViewController dismissViewControllerAnimated:NO completion:nil];
        [viewController removeFromParentViewController];

        // Tell the delegate that the user successfully logged in ...
    } else {
        // Error handling code ...
    }
}
Run Code Online (Sandbox Code Playgroud)


gro*_*ins 0

使用 gtm-oauth2 登录 Google 服务时,请确保Google API 控制台项目注册在 API 访问部分显示客户端 ID 是为已安装的应用程序颁发的。gtm-oauth2文档对此进行了描述。