首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用ASWebAuthenticationSession获取验证码?

如何使用ASWebAuthenticationSession获取验证码?
EN

Stack Overflow用户
提问于 2020-02-26 00:00:37
回答 3查看 4.2K关注 0票数 5

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

不幸的是,authSession的完成处理程序不会使用callbackURL回调。我需要这个callbackURL来查询授权码。我读过关于这个主题的不同文章,但我不明白为什么我的实现不能像我期望的那样工作。

下面是我的代码:

代码语言:javascript
复制
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()
    }
}
EN

回答 3

Stack Overflow用户

发布于 2020-02-26 02:35:38

我认为问题是你用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

票数 2
EN

Stack Overflow用户

发布于 2020-04-23 18:46:25

我知道这很老,但不管怎样。确保redirect_uri中使用的callbackScheme和方案是相同的。

票数 1
EN

Stack Overflow用户

发布于 2021-06-18 23:04:58

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

不能在URI的方案名称中使用符号:

请参阅以下SchemeRFC定义。

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

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60398648

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档