在iOS上使用OAuth2进行身份验证

我目前正在尝试使用OAuth2授权我的用户。我当前正在使用以下库:https : //github.com/p2/OAuth2

let oauth2 = OAuth2CodeGrant(settings: [
        "client_id": "my-id",
        "authorize_uri": "https://accounts.google.com/o/oauth2/auth",
        "token_uri": "https://www.googleapis.com/oauth2/v3/token",
        "scope": "profile",     // depends on the API you use
        "redirect_uris": ["com.TestAuthorizeApp:/oauth2Callback"],
        ])

    //let oauth2 = OAuth2CodeGrant(settings: settings)
    oauth2.onAuthorize = { parameters in
        print("Did authorize with parameters: \(parameters)")
    }
    oauth2.onFailure = { error in        // `error` is nil on cancel
        if let error = error {
            print("Authorization went wrong: \(error)")
        }
    }

    oauth2.authConfig.authorizeEmbedded = false
    oauth2.authorize()

当我运行它时,它会在浏览器中加载google,并且我能够登录。然后它询问我在范围中声明的权限,并且效果很好。我单击“确定打开”,它会将我重定向回我的应用程序。

但是,当我再次运行此代码时,我期望访问令牌已存储在密钥链中。但是,这似乎不起作用。

我查看了源代码,发现以下检查:tryToObtainAccessTokenIfNeeded始终返回false。这意味着我将再次获得需要单击“允许”的页面。

我想知道是否有人可以帮助我弄清楚为什么它没有在钥匙串中保存任何东西。这是否也意味着用户没有真正通过身份验证?

谢谢。

===

编辑

oauth2.verbose = true根据Pascal的评论添加了我得到以下输出。

 OAuth2: Looking for items in keychain
 OAuth2: No access token, maybe I can refresh
 OAuth2: I don't have a refresh token, not trying to refresh

我以为这是正在发生的事情。但是我仍然不确定为什么它不能在钥匙串中保存/查找任何东西。

=====

编辑2

事实证明,我实际上根本没有获得访问令牌。请查看此对话:https : //github.com/p2/OAuth2/issues/109和下面的我的答案。

在Pascal的帮助下:https : //github.com/p2/OAuth2/issues/109,我设法使其正常运行。原来我没有执行步骤:像我应该做的那样“ 3授权用户”。

因此,一个完整的解决方案是:

在我的视图控制器中,我有以下内容:

let OAuth2AppDidReceiveCallbackNotification = "OAuth2AppDidReceiveCallback"

override func viewDidLoad() {
    super.viewDidLoad()

    // This notification is for handling step 3 in guide.
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.handleRedirect(_:)), name: OAuth2AppDidReceiveCallbackNotification, object: nil)
}

func authoriseUser {
    let oauth2 = OAuth2CodeGrant(settings: [
        "client_id": "my-id", // Use own client_id here
        "authorize_uri": "https://accounts.google.com/o/oauth2/auth",
        "token_uri": "https://www.googleapis.com/oauth2/v3/token",
        "scope": "profile",     // depends on the API you use
        "redirect_uris": ["com.TestAuthorizeApp:/oauth2Callback"],
        ])

     //let oauth2 = OAuth2CodeGrant(settings: settings)
     oauth2.onAuthorize = { parameters in
        print("Did authorize with parameters: \(parameters)")
     }
     oauth2.onFailure = { error in        // `error` is nil on cancel
         if let error = error {
             print("Authorization went wrong: \(error)")
         }
     }

     oauth2.authConfig.authorizeEmbedded = false
     oauth2.authorize()
 }

// This method gets called by notification and is the last thing we need to do to get our access token. 
func handleRedirect(notification: NSNotification) {
    oauth2.handleRedirectURL(notification.object as! NSURL)
}

上面的代码应可以将您发送到Google网页,您可以在该网页上登录,然后单击“允许”。

现在,您需要在应用程序委托中处理返回到应用程序的操作:

 let OAuth2AppDidReceiveCallbackNotification = "OAuth2AppDidReceiveCallback"

 func application(application: UIApplication,
                 openURL url: NSURL,
                         sourceApplication: String?,
                         annotation: AnyObject) -> Bool {
    // you should probably first check if this is your URL being opened
    NSNotificationCenter.defaultCenter().postNotificationName(OAuth2AppDidReceiveCallbackNotification, object: url)

    return true
}

希望这将帮助可能遇到其他问题的其他人,试图获取访问令牌。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用OAuth2以编程方式向Google进行身份验证

无法使用包“ golang.org/x/oauth2”与facebook进行身份验证:“缺少redirect_uri参数”

使用软件包“ golang.org/x/oauth2”对oauth2进行身份验证

如何使用Spring Boot伪装客户端进行Oauth2身份验证?

OAuth2对API进行身份验证-无法重定向回我的应用

使用OAuth2在Office 365中进行IMAP身份验证

使用Spring Security oauth2进行两因素身份验证

OAuth2:使用电子邮件(而不是用户名)进行身份验证

IMAP如何使用OAuth或OAuth2身份验证访问Yahoo Mail

使用Spring Security进行gRPC和OAuth2身份验证

实施对API进行oAuth2身份验证的调用的指南

使用OAuth2进行Geoserver身份验证

如何使用C#对Picasa网络相册数据API进行OAuth2身份验证?

如何使用REST API使用OAuth2和React Native进行身份验证?

邮递员:如何为使用Google OAuth2的Web应用程序进行身份验证?

使用第三方OAUTH2服务器进行SuiteCRM身份验证

没有OIDC的OAuth2中如何进行身份验证?

无法对Azure上的API管理进行资源所有者密码OAuth2身份验证

自动进行OAUTH2身份验证

使用Azure AD通过OAuth2对Azure API管理进行身份验证

API身份验证和OAuth2的使用

在iOS中使用NSURLSessions对服务器API进行oAuth2身份验证

是否可以仅使用Marketo使用用户名和密码进行OAuth2身份验证?

使用MVC5网站失败的Google OAuth2进行身份验证(GetExternalLoginInfo始终为null)

使用Oauth2在Flask中对具有特定托管域(hd)的用户进行身份验证

使用Spring Boot进行OAuth2身份验证后无法正确重定向

使用 oauth2 进行静默身份验证(提示 = 无)

如何在 Google Analytics Reporting API 中使用 OAuth2 进行身份验证?

我需要进行 oauth2 身份验证才能在 C# 中访问 gmail