简体   繁体   English

授权回调URL GitHub

[英]Authorization callback URL GitHub

I'm beginning iOS developer. 我正在开始iOS开发。 I create an application that uses GitHub authorization. 我创建一个使用GitHub授权的应用程序。 When I register a new OAuth application in GitHub developer program I must enter Authorization callback URL. 在GitHub开发人员程序中注册新的OAuth应用程序时,必须输入授权回调URL。 But I do not have any site for my app. 但是我的应用程序没有任何网站。 What do I need to specify in this field? 我需要在此字段中指定什么?

You can use deep linking. 您可以使用深层链接。

you can read more about it here 你可以在这里了解更多

The deeplink will try to open the app or redirect to it. 深度链接将尝试打开应用或将其重定向到该应用。 The web browser or SFAuthenticationSession will close the browser and call the completion hander where you can check for the response code without any implementation for the deeplink. Web浏览器或SFAuthenticationSession将关闭浏览器并调用完成处理程序,您可以在其中检查响应代码而无需对Deeplink进行任何实现。

To add the deep link in the app you can this below: 要在应用中添加深层链接,您可以在下面进行以下操作:

  • Select the project in Xcode navigator. 在Xcode导航器中选择项目。
  • then select your target that you want to add the deep link to it. 然后选择要向其添加深层链接的目标。
  • select info from the top bar 从顶部栏中选择信息
  • at the bottom open the URL Types 在底部打开“ URL类型”
  • add a name for the scheme 为方案添加名称

添加深层链接

when you generate the URL for the oauth you can pass anything you want I just pass login in this example: 当您为oauth生成URL时,您可以传递任何您想要的信息,我只是在此示例中传递了登录信息:

func getAuthenticateURL() -> URL {

    var urlComponent = URLComponents(string: "https://github.com/login/oauth/authorize")!

    var queryItems =  urlComponent.queryItems ?? []

    queryItems.append(URLQueryItem(name: "client_id", value: "YOUR_CLIENT_ID_HERE"))
    queryItems.append(URLQueryItem(name: "redirect_uri", value: "APP_SCHEME_GOES_HERE://login"))

    urlComponent.queryItems = queryItems

    return urlComponent.url!
}

Then when you need to login do this: 然后,当您需要登录时,请执行以下操作:

import SafariServices

var authSession: SFAuthenticationSession?

func authenticate(with url: URL, completion: @escaping ((_ token: String?, _ error: Error?) -> Void)) {

    authSession?.cancel()

    authSession = SFAuthenticationSession(url: url, callbackURLScheme: nil, completionHandler: { url, error in

        //get the token and call the completion handler
    })

    authSession?.start()
}

or use ASWebAuthenticationSession the same way if you're on iOS 12 或以相同方式使用ASWebAuthenticationSession如果您使用的是iOS 12)

Using SFAuthenticationSession you can do something like this. 使用SFAuthenticationSession,您可以执行以下操作。 On your App add URLType : 在您的应用上添加URLType

在此处输入图片说明

Then on GitHub 'Developer Settings' for your app, add Authorization callback URL like this: 然后在您的应用程序的GitHub'Developer Settings'上添加如下的Authorization回调URL

在此处输入图片说明

This way, after you login and authorize, Git Hub will call back //yourappname and Safari will redirect it back to your app completing the flow. 这样,在您登录并授权后,Git Hub将回调// yourappname,而Safari会将其重定向回您的应用程序,以完成流程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM