【问题标题】:How to get an auth code using ASWebAuthenticationSession?如何使用 ASWebAuthenticationSession 获取授权码?
【发布时间】:2020-06-09 10:11:53
【问题描述】:

我正在尝试使用 ASWebAuthenticationSession 从Stripe's OAuth endpoint 获取身份验证代码 - 此事件发生在调用我的 Stripe 重定向 url 之后。

不幸的是,authSession 的完成处理程序没有使用回调 URL 进行回调。我需要这个 callbackURL 来查询授权码。我已经阅读了关于这个主题的不同文章,但我不明白为什么我的实现不能按照我期望的方式工作。

这是我的代码:


class CreateStripeConnectAccountController: UIViewController {

  var authSession: ASWebAuthenticationSession!

  override func viewDidLoad() {
      super.viewDidLoad()
      configureAuthSession()
  }

  private func configureAuthSession() {

    let urlString = Constants.URLs.stripeConnectOAuth // Sample URL

    guard let url = URL(string: urlString) else { return }

    let callbackScheme = "myapp:auth"    

    authSession = ASWebAuthenticationSession(url: url, callbackURLScheme: callbackScheme, completionHandler: { (callbackURL, error) in
       guard error == nil, let successURL = callbackURL else {
          print("Nothing")
          return
       }

       let oauthToken = NSURLComponents(string: (successURL.absoluteString))?.queryItems?.filter({$0.name == "code"}).first

       print(successURL.absoluteString)
    })

    authSession.presentationContextProvider = self
    authSession.start()
  }
}

extension CreateStripeConnectAccountController: ASWebAuthenticationPresentationContextProviding {
    func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
        self.view.window ?? ASPresentationAnchor()
    }
}

【问题讨论】:

  • 如果没有人回答这个问题,请考虑在他们的 Github 上发布这个问题(如果有的话)。
  • @Glenn 好主意
  • @uchennaaguocha 您是否为您的应用启用了通用深度链接?这里的替代方法是在 webviews 中完全处理 OAuth,然后让您的“redirect_url”页面手动链接回您的 iOS 应用程序此外,当您在 AuthContext 中完成 OAuth 流程时,您的应用程序会发生什么?预计会打开您在 Connect 设置中指定的重定向 url
  • @hmunoz 我最初的方法涉及 webview。我的用户将创建一个 Stripe Connect 帐户,一旦完成,就会调用重定向 url。它会将用户重定向回应用程序。本周早些时候,我的应用被拒绝,因为 Apple 弃用了 UIWebview API。所以,这就是我使用 ASWebAuthenticationSession 而不是 webview 的原因。
  • @uchennaaguocha 了解详情!我也在做同样的事情并且它正在工作。即 Stripe Connect 页面 -> 重定向 URL -> 该页面深层链接到我的 iOS 应用程序。您能否仔细检查您的 url 方案以及重定向页面重定向到的 url 方案?你现在也可以使用 WKWebView 代替 UIWebView

标签: ios swift oauth stripe-payments aswebauthenticationsession


【解决方案1】:

我认为问题在于您为callbackURLScheme 提供了nil。您需要提供一个重定向到您的应用的 URL 方案:

查看苹果的认证示例:https://developer.apple.com/documentation/authenticationservices/authenticating_a_user_through_a_web_service

这里是关于如何为您的应用创建自定义 URL 方案的苹果文档:https://developer.apple.com/documentation/uikit/inter-process_communication/allowing_apps_and_websites_to_link_to_your_content/defining_a_custom_url_scheme_for_your_app

【讨论】:

  • 感谢您的建议。我现在要研究一下。
  • 是的,让我知道。这对我来说似乎是最可能的原因,但我可能错了!
  • @wilco0 我试过了,但对我没有用。我使用我的自定义 url 方案创建了一个 URL 类型,并称为 authSession.start()。到身份验证完成时,authSession 回调/完成处理程序没有被触发。
  • 您介意发布您的更新代码以便我查看吗?
  • 我更新了我原来的帖子。您应该看到一个名为“callbackScheme”的常量。这与我用作 URL 类型的 url 方案相同。
【解决方案2】:

我知道它很旧,但无论如何。 确保callbackSchemeredirect_uri 中使用的方案相同。

【讨论】:

    【解决方案3】:

    您的 callbackScheme myapp:auth 格式不正确。

    符号 : 不能用于 URI 的方案名称。

    请参阅以下RFCScheme 的定义。

    方案名称由一系列以 a 开头的字符组成 字母,后跟字母、数字和加号的任意组合 ("+")、句点 (".") 或连字符 ("-")。

    https://datatracker.ietf.org/doc/html/rfc3986#section-3.1

    因此,将callbackURLscheme 修改为myapp-authmyapp.auth 效果很好。

    【讨论】:

      猜你喜欢
      • 2020-04-28
      • 2020-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-16
      • 1970-01-01
      • 2018-01-12
      • 2013-09-30
      相关资源
      最近更新 更多