gpt4 book ai didi

android - 如何从浏览器 Intent 中获取授权 token ?

转载 作者:行者123 更新时间:2023-11-29 16:06:21 25 4
gpt4 key购买 nike

抱歉我的英语不好,我会尽可能简单地解释我的问题。我正在尝试制作一个适用于 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=YYYYYYYYYYYYYY&token_type=code...”等等。但我没有看到此页面,因为它立即重定向回应用。

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

非常抱歉,我太笨了,希望你能帮助我。提前致谢

最佳答案

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

@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];
}

关于android - 如何从浏览器 Intent 中获取授权 token ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17979589/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com