Strava身份验证未调用ASWebAuthenticationSessioncompleteHandler

时间:2020-01-31 08:42:30

标签: ios swift strava aswebauthenticationsession

在Strava身份验证中单击“授权”按钮时未调用

ASWebAuthenticationSessioncompleteHandler。下面是我的代码。(使用Using ASWebAuthenticationSession to connect to Strava account fails此链接实现。)

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

和我的info.plist文件看起来像 enter image description here

2 个答案:

答案 0 :(得分:1)

我是通过更改代码来实现的。.

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

enter image description here

<client id>更改为客户端ID,将abcd更改为应用程序名称。.

答案 1 :(得分:0)

我认为您将 URL Scheme 格式更改为 abcd 是正确的。但是,初始化callbackURLScheme时传递的ASWebAuthenticationSession参数的值必须与这个URL Scheme(abcd)相同。因此,您的代码应更改如下:

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

 })

您也可以参考Apple's official documentation了解如何使用ASWebAuthenticationSession