抱歉我的英语不好,我会尽力尽可能简单地解释我的问题。
我正在尝试制作一个与 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);
}
这将启动浏览器并重定向到授权页面。输入登录密码后,我会自动返回到我的应用程序。这是 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>
当我输入登录密码时,我被重定向到类似的页面
“yaruapp://token#access_token=YYYYYYYYYYYYYYY&token_type=code...”等等。但我没有看到此页面,因为立即重定向回应用程序。
问题是:我怎样才能获取并提取这部分:YYYYYYYYYYYYYY?
我非常抱歉我这么菜鸟,希望你能帮助我。提前致谢
你可以在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];
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)