V. Khuraskin
V. Khuraskin

Reputation: 89

Authorization callback URL GitHub

I'm beginning iOS developer. I create an application that uses GitHub authorization. When I register a new OAuth application in GitHub developer program I must enter Authorization callback URL. But I do not have any site for my app. What do I need to specify in this field?

Upvotes: 2

Views: 4549

Answers (2)

Marcos Reboucas
Marcos Reboucas

Reputation: 3489

Using SFAuthenticationSession you can do something like this. On your App add URLType:

enter image description here

Then on GitHub 'Developer Settings' for your app, add Authorization callback URL like this:

enter image description here

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.

Upvotes: 2

zombie
zombie

Reputation: 5269

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.

To add the deep link in the app you can this below:

  • Select the project in Xcode navigator.
  • 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
  • add a name for the scheme

add deeplink

when you generate the URL for the oauth you can pass anything you want I just pass login in this example:

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

Upvotes: 3

Related Questions