本文介绍了使用 ASWebAuthenticationSession 连接 Strava 帐户失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用用于 iOS 应用程序中的 OAuth2 进程的新 ASWebAuthenticationSession 类时遇到问题.

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

我想使用这个类将我的应用与我的 Strava 帐户相关联.我有以下代码:

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
    }
}

我将自定义 URL 方案添加到我的 info.plist 文件中.我将示例代码中的回调 url 更改为test.com",但实际上这与在我的 Strava 应用程序设置中用作回调 url 的 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.

当我调用 authenticate() 函数时,会弹出一个对话框并打开 Strava 站点.当我点击授权"时,completionHandler 不会被调用(只有当我点击取消时才会被调用).

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).

我必须添加一些不同的东西吗?我的回调网址有效吗?我尝试了回调 URL 方案和我在 Strava 中设置的回调 URL 的几种组合,但没有任何效果.

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.

推荐答案

因为我非常确定我的代码是正确的.我通过打开 Safari 并输入用作回调 url 方案的自定义 URL 方案对此进行了测试.显示了一个提示 - 所以我知道自定义 URL 方案设置正确.

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.

现在我尝试清除 Safari 缓存,这似乎解决了问题.

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.

这篇关于使用 ASWebAuthenticationSession 连接 Strava 帐户失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 14:51