1

我用过这个库。 https://github.com/mpclarkson/StravaSwift

我的回调 url 将我重定向到任何浏览器链接,但不会将我重定向到我的应用程序。如何在通过 strava 身份验证后重定向回我的应用程序?

4

2 回答 2

0

要将 strava 重定向回您的应用程序,您需要在 info.plist 文件中注册一个 url 方案,然后使用该 url 方案来允许操作系统识别重定向。

以下是一些应该有所帮助的步骤:

  1. 在 info.plist 中注册 url 方案。为此,请LSApplicationQueriesSchemes在 info.plist 下添加一个唯一条目,例如“myApp”。如果您还没有LSApplicationQueriesSchemesinfo.plist 作为条目,则需要创建它(需要是一个数组)。

  2. 按照 myApp.com 的方式在https://www.strava.com/settings/api向 Strava 注册授权回调域。

  3. 分配 callbackURLScheme 以匹配 info.plist 中注册的 url 方案(来自步骤 1,“myApp”)。为了显示:

private var authSession: ASWebAuthenticationSession?

let appOAuthUrlStravaScheme = URL(string: "strava://oauth/mobile/authorize?client_id=1234321&redirect_uri=myApp%3A%2F%2FmyApp.com&response_type=code&approval_prompt=auto&scope=activity%3Awrite%2Cread&state=test")!

let webOAuthUrl = URL(string: "https://www.strava.com/oauth/mobile/authorize?client_id=1234321&redirect_uri= myApp%3A%2F%2FmyApp.com&response_type=code&approval_prompt=auto&scope=activity%3Awrite%2Cread&state=test")!


func authenticate() {
    if UIApplication.shared.canOpenURL(appOAuthUrlstravaScheme) {
        UIApplication.shared.open(appOAuthUrlstravaScheme, options: [:])
    } else {
        authSession = ASWebAuthenticationSession(url: webOAuthUrl, callbackURLScheme: "myApp") { url, error in
            // Code after redirect
        }

        authSession?.start()
    }
}
于 2020-10-16T20:35:53.877 回答
0

OP 提到使用这个 GitHub 可可豆荚 -> https://github.com/mpclarkson/StravaSwift

使用 GitHub 项目中的示例应用程序,以下是在 Strava oAuth 身份验证后重定向回应用程序所需的内容。

信息列表

<key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleTypeRole</key>
            <string>Editor</string>
            <key>CFBundleURLName</key>
            <string>com.stravaswift.ios</string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>breakaway</string> <--- This is my App Name
            </array>
        </dict>
    </array>

请注意,根据该项目页面的 readme.md 文件,您还需要这个

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>strava</string>
</array>

然后在 appDelegate.swift 中(也根据 readme.md)

  let config = StravaConfig(
        clientId: <YOUR_CLIENT_ID>,
        clientSecret: <YOUR_CLIENT_SECRET>,
        redirectUri: "breakaway://breakaway701849232.wordpress.com",
    scopes: [.activityReadAll,.activityWrite]
    )

为 IOS14.5+ / Xcode 12.5+ 编辑由于 ASWebuthentitor https://developer.apple.com/forums/thread/679251的更改)我收到上述错误。redirectUri 现在更改为以下

  let config = StravaConfig(
        clientId: <YOUR_CLIENT_ID>,
        clientSecret: <YOUR_CLIENT_SECRET>,
  redirectUri: "breakaway://breakaway701849232.wordpress.com".addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!,
    scopes: [.activityReadAll,.activityWrite]
    )

请注意,redirectUri 是“breakaway”://“breakaway701849232.wordpress.com”,其中:breakaway - 我的 redirectUri breakaway701849232.wordpress.com - 我的域(输入到我的应用程序的 Strava API 中)

在此处输入图像描述

注意:我遇到的一个问题是 Scopes 实际上是一个逗号分隔的数组。我在使用列出所有活动的示例应用程序时不断收到错误,但这是因为我没有在范围内包含“ReadAll”(我的目标是将 TCX 文件上传到 Strava)。

于 2020-12-01T14:46:47.420 回答