Square API + OAuth2:如何将重定向网址设置为我的iOS应用?

时间:2016-10-26 21:55:56

标签: ios oauth2 square square-connect

我正试图通过Square在我的iOS应用中实现OAuth2,但是当我通过弹出的浏览器成功登录时,我的redirect_uri有错误。

enter image description here

我正在使用OAuthSwift pod。这是我到目前为止设置URL方案,以便重定向应该打开我的iOS应用程序:

方形仪表板配置:

enter image description here

的AppDelegate:

class AppDelegate: UIResponder, UIApplicationDelegate {
  var window: UIWindow?

  func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
    print("hollaaaaaaaaaaaaaaaaa") // i never see this printed
    OAuthSwift.handle(url: url)
    return true
  }
}

目标:

enter image description here

打开浏览器的控制器:

class OAuthViewController: UIViewController {

  @IBAction func signInButtonTapped(_ sender: AnyObject) {
    print("tapped");

    let oauthswift = OAuth2Swift(
        consumerKey:    "my token",
        consumerSecret: "my secret",
        authorizeUrl:   "https://connect.squareup.com/oauth2/authorize?client_id=my_id",
        responseType:   "token"
    )
    oauthswift.authorize(
      withCallbackURL: URL(string: "com.edmund.ios/oauth-callback")!, // doesn't seem to do anything honestly... I think the Square dashboard setting has precedence over this.
      scope: "MERCHANT_PROFILE_READ%20PAYMENTS_READ%20ITEMS_READ%20ORDERS_READ",
      state: "",
      success: { (credential, response, parameters) -> Void in
        print(credential)
      },
      failure: { error in
        print(error.localizedDescription)
      }
    )
  }

}

3 个答案:

答案 0 :(得分:1)

当您引导用户完成应用程序的oauth流时,您必须指定一个redirect_uri参数,该参数与您在Square开发人员门户中指定的值相匹配。请注意,此redirect_uri必须以http或https开头,并且对应于服务器上的网页。

答案 1 :(得分:0)

如果将方形端点重定向到您的服务器,并且确定它们在iOS上运行,则可以使用URL方案重新打开您的应用并传递所需的任何参数

答案 2 :(得分:0)

可以重定向到ios应用吗?完全有可能

在这里,我将指导您实现这一目标的简单方法。 oAuth方形实现可以通过2个简单的简单步骤来实现,而无需使用任何第三方库。

这种方法的优点

  • 您始终留在应用程序中(因为我们使用应用程序内浏览器)
  • 无需在应用程序中添加URI模式(因为我们从不离开应用程序)

  • 第1步:添加视图控制器并附加WKWebview;
  • 第2步:加载身份验证请求URL并监听重定向URI;

重定向发生后,您可以关闭控制器并继续使用访问令牌。


重定向URI

您必须在方形信息中心中设置重定向URI; (Example: "http://localhost/square-oauth-callback") 但您可以自由设置任何有效的URL。 我们在我们的应用程序中监视此URL。 enter image description here


在您的应用程序中实现以下代码

import Foundation
import UIKit
import WebKit


class SquareAuthenticationViewController: UIViewController {


    // MARK: Connection Objects
    @IBOutlet weak var webView: WKWebView!



    // MARK: Life Cycle
    override func viewDidLoad() {
        super.viewDidLoad()

        configureView()
        initiateAuthentication()
    }



    func configureView() {

        webView.navigationDelegate = self
    }



    func initiateAuthentication() {

        // Validation
        guard let url = getPath() else {
            return
        }

        // Prepare request
        let request = URLRequest(url: url)
        webView.load(request)
    }



    func getPath() -> URL? {

        let clientId = "Your Suare Application Id"
        let scope = ["MERCHANT_PROFILE_READ",
                     "CUSTOMERS_READ",
                     "CUSTOMERS_WRITE",
                     "EMPLOYEES_READ",
                     "EMPLOYEES_WRITE",
                     "ITEMS_READ",
            "PAYMENTS_READ"].joined(separator: " ")
        let queryClientId = URLQueryItem(name: "client_id" , value: clientId.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed))
        let queryScope = URLQueryItem(name: "scope" , value: scope.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed))

        var components = URLComponents()
        components.scheme = "https"
        components.host = "connect.squareup.com"
        components.path = "/oauth2/authorize"
        components.percentEncodedQueryItems = [queryClientId, queryScope]
        return components.url
    }
}



extension SquareAuthenticationViewController: WKNavigationDelegate {

    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {

        // here we handle internally the callback url and call method that call handleOpenURL (not app scheme used)
        if let url = navigationAction.request.url, url.host == "localhost" , url.path == "/square-oauth-callback" {


            print(url)
            print(url.valueOf("code"))
            //decisionHandler(.cancel)

            /*  Dismiss your view controller as normal
                And proceed with OAuth authorization code
                The code you receive here is not the auth token; For auth token you have to make another api call with the code that you received here and you can procced further
             */

            /*
                Auth Process Flow: https://developer.squareup.com/docs/oauth-api/how-it-works#oauth-access-token-management

                Obtain Auth Token: https://developer.squareup.com/reference/square/oauth-api/obtain-token
             */

        }
        decisionHandler(.allow)
    }

}


extension URL {

    func valueOf(_ queryParamaterName: String) -> String? {

        guard let url = URLComponents(string: self.absoluteString) else { return nil }
        return url.queryItems?.first(where: { $0.name == queryParamaterName })?.value
    }
}