Android Chrome 自定义选项卡/Fitbit web API 如果应用程序已获得授权,则不会重定向。 (OAuth2.0)

Android Chrome custom tabs / Fitbit web API won't redirect if app is already authorized. (OAuth2.0)

我打算创建第三方 fitbit 应用程序来同步闹钟。

但是,我在注册我的应用程序时遇到了一些困难,更明确地说,即使我的客户已经注册到该应用程序,也很难获取访问令牌。 (考虑到用户重新安装他的应用程序的场景)。

我正在使用 Chrome 自定义选项卡(因为 WebView 被 FitBit 禁止)来请求访问令牌:

String url = "https://www.fitbit.com/oauth2/authorize?" +
                    "response_type=token" +
                    "&client_id=XXXXXX" +
                    "&scope=activity"+
                    "&redirect_uri=fitbittester://logincallback";
            customTabsIntent.launchUrl(MainActivity.this, Uri.parse(url));

重定向到使用 intent-filter 定义的自定义方案时:

<activity
        android:name=".TestActivity"
        android:label="TestActivity"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.BROWSABLE"/>
            <data android:scheme="fitbittester" android:host="logincallback" />
        </intent-filter>
    </activity>

TestActivity 应该启动,我将从给定的 Intent 中获取我的 AccessToken:

public class TestActivity extends AppCompatActivity {

String string;

@Override
protected void onNewIntent(Intent intent) {
    string = intent.getDataString();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    onNewIntent(getIntent());
    Toast.makeText(TestActivity.this, string , Toast.LENGTH_LONG).show();
    Log.e("TAG", string);
    Log.e("TAG", string.substring(string.indexOf("&access_token")+14));
}

}

第一个 运行 一切正常(前提是客户端尚未获得授权),但之后如果想再次获取我的访问令牌(我知道我应该将其存储在本地 - SharedPreferences最有可能,但这仅用于测试目的)chrome 自定义选项卡将打开并停留在空白页面上(显然它不会正确重定向)。

我已经阅读了 FitBit WEB API,它说了以下内容: 如果使用隐式授予流程的应用程序在先前颁发的访问令牌过期之前将用户发送到授权页面,则除非范围增加,否则不会提示用户。用户将立即被重定向到具有访问令牌的应用程序。

所以我的问题是我对这个问题的思考是否有错误或者
我应该拦截 chrome 自定义标签错误吗?

非常感谢您。

我找到了解决此问题的方法。 基本上,我在 Url 中插入了一个新参数,用于查询 Fitbit API。 (“&提示=登录”)。该参数会在用户每次查询授权令牌时提示用户重新登录,如果已经登录则注销。

所以我猜想当用户已经登录时 fitbit 会进行 302 重定向。所以我使用了这个解决方案(将这个解决方案与 Chrome tab demo 的 CustomTabActivityHelper 混合使用)并解决了这个问题。耶。

I was able to "fix" the issue by calling the warmup function before loading the url that redirects.