Using ASWebAuthenticationSession to connect to Strava account fails

只愿长相守 提交于 2020-03-03 12:24:26

问题


I have trouble using the new ASWebAuthenticationSession class that is used for the OAuth2 process in iOS applications.

I want to use this class to connect my app with my Strava account. I have the following code:

class AuthController
{
    private var authSession: ASWebAuthenticationSession?

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

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

        })

        self.authSession?.start()
    }

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

        url.append("https://www.strava.com/oauth/mobile/authorize")
        url.append("?client_id=000000") // Removed for stackoverflow...
        url.append("&redirect_uri=MaccaStravaWebAuth://test.com")
        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
    }
}

I added the custom URL scheme to my info.plist file. I changed the callback url in the sample code to "test.com", but in reality this is the exact same URL that is also used in my Strava application settings as the callback url.

When I call the authenticate() function, a dialog pops up and the Strava site is opened. When I click "Authorise", the completionHandler is not called (it is only called when I click Cancel).

Do I have to add something different? Are my callback urls valid? I tried several combinations of the callback URL scheme and my callback url that is set in Strava, but nothing worked.


回答1:


Because I was pretty sure that my code was correct. I tested this by opening Safari and typing in my custom URL scheme that is used as the callback url scheme. A prompt was shown - so I knew that the custom URL scheme is set up properly.

Now I tried to clear the Safari cache and this seems to solve the problem.

Now after I click the Authorize button, my app will be reopened.



来源:https://stackoverflow.com/questions/55870833/using-aswebauthenticationsession-to-connect-to-strava-account-fails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!