简体   繁体   中英

ASWebAuthenticationSession completionHandler not called on Strava Authentication

ASWebAuthenticationSession completionHandler not called on clicking Authorize button in Strava Authentication. below given is my code.(used Using ASWebAuthenticationSession to connect to Strava account fails this link to implement.)

import AuthenticationServices

class LinkAppsViewController: UIViewController, ASWebAuthenticationPresentationContextProviding{

func authenticate()
    {
        let string = self.createWebAuthUrl()
        guard let url = URL(string: string) else { return }

        self.authSession = ASWebAuthenticationSession(url: url, callbackURLScheme: "localhost/", completionHandler:
        {
            url, error in

        })

        self.authSession!.presentationContextProvider = self

        self.authSession?.start()
    }

    fileprivate func createWebAuthUrl() -> String
    {
        var url = String.init()

        url.append("https://www.strava.com/oauth/mobile/authorize")
        url.append("?client_id=<client id>")
        url.append("&redirect_uri=http://localhost/exchange_token")
        url.append("&response_type=code")
        url.append("&approval_prompt=force")
        url.append("&grant_type=authorization_code")
        url.append("&scope=activity:write,activity:read_all")

        return url
    }

    func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
        print(session)
        return view.window!
    }
}

and my info.plist file looks like在此处输入图片说明

I got it by changing my code like..

fileprivate func createWebAuthUrlStrava() -> String
{
    var url = String.init()

    url.append("https://www.strava.com/oauth/mobile/authorize")
    url.append("?client_id=<client id>")
    url.append("&redirect_uri=abcd://localhost/exchange_token")
    url.append("&response_type=code")
    url.append("&approval_prompt=force")
    url.append("&grant_type=authorization_code")
    url.append("&scope=activity:write,activity:read_all")

    return url
}

info.plist

在此处输入图片说明

Change <client id> with your client id and abcd with your app name..

I think you are correct in changing the URL Scheme format to abcd . However, the value of the callbackURLScheme parameter passed when initializing ASWebAuthenticationSession must be the same as this URL Scheme( abcd ). Therefore, your code should be changed as follows:

self.authSession = ASWebAuthenticationSession(url: url, callbackURLScheme: "abcd", completionHandler:
{ url, error in

 })

You may also refer to Apple's official documentation about how to use ASWebAuthenticationSession .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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