如何从浏览器意图获取授权令牌?

Gro*_*sha 2 android authorization oauth

抱歉我的英语不好,我会尽力尽可能简单地解释我的问题。我正在尝试制作一个与 Yandex API 配合使用的应用程序。在他们的帮助页面上,我读到您应该从用户登录的应用程序启动浏览器,然后通过注册 URI 回调返回到应用程序。我现在拥有的:

 @Override
 public void onClick(View view) {
 Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://oauth.yandex.ru/authorize?response_type=token&client_id=XXXXXXXXXXXXXXX"));
 startActivity(browserIntent);
}
Run Code Online (Sandbox Code Playgroud)

这将启动浏览器并重定向到授权页面。输入登录密码后,我会自动返回到我的应用程序。这是 AndroidManifest:

 <activity
            android:name="ru.mastergroosha.yaruclient.Main"
            android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
    <activity
            android:name="ru.mastergroosha.yaruclient.Window"
            android:label="Window">
        <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="yaruapp"
                    android:host="token"/>
        </intent-filter>
    </activity>
Run Code Online (Sandbox Code Playgroud)

当我输入登录密码时,我被重定向到“yaruapp://token#access_token=YYYYYYYYYYYYYYYY&token_type=code...”等页面。但我没有看到此页面,因为立即重定向回应用程序。

问题是:我如何获取并提取这部分: YYYYYYYYYYYYYY ?

我非常抱歉我这么菜鸟,希望你能帮助我。提前致谢

FD_*_*FD_ 6

你可以在onNewIntent中获取Uri。只需在您的 Activity 中覆盖它即可。您可以通过以下方式获取访问令牌:

@Override
protected void onNewIntent (Intent intent){
  Uri uri = intent.getData();
  if (uri!=null){
    String mainPart = uri.toString().split("#")[1];
    String[] arguments = mainPart.split["&"];
    String argument = arguments[0];
    String token = argument.split("=")[1];
}
Run Code Online (Sandbox Code Playgroud)