android - 如何在 oauth 成功时重定向到 Activity A 或在 oauth 失败时重定向到 Activity B
问题描述
我有三个活动 A、B 和 C。当在活动 C 中单击一个按钮时,它会打开一个浏览器意图,用户可以批准或拒绝该请求。当用户批准请求时,我想重定向回应用程序并打开活动 A,但如果用户拒绝请求,我也想重定向到应用程序但活动 B。
我已经成功地重定向回应用程序,但我不知道如何重定向到 A 或 B。
在此处的 api 文档中,它说当用户单击批准时,如果指定,他们将被重定向到自定义 url,但它没有说明用户是否拒绝请求。
这是我的代码
token = response.getString("request_token");
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.themoviedb.org/authenticate/" + token + "?redirect_to=schemeName://hostName/path"));
startActivity(browserIntent);
清单.xml
<activity android:name=".LoginActivity" android:theme="@style/NoActionBar"
android:configChanges="orientation" android:screenOrientation="portrait">
<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:host="hostname"
android:path="/path"
android:scheme="schemename" />
</intent-filter>
</activity>
解决方案
您可以启动结果的身份验证活动。这样,您可以将数据从第一个活动传递到第二个活动,并根据结果将它们返回到正确的活动。
在活动 C 中:
startActivityForResult(browserIntent, a_unique_integer_code);
在浏览器活动中:
//if authentication succeeds
Intent returnIntent = new Intent();
setResult(Activity.RESULT_OK, returnIntent);
finish();
//else:
Intent returnIntent = new Intent();
setResult(Activity.RESULT_CANCELED, returnIntent);
finish();
并在您的活动 C 中接受结果并根据您获得的内容开始所需的活动:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == a_unique_integer_code) {
if(resultCode == Activity.RESULT_OK){
//start activity a
}
if (resultCode == Activity.RESULT_CANCELED) {
//start activity b
}
}
}
推荐阅读
- oauth - OAuth2 功能与 2FA?
- c - 为什么不能将指向整数数组的指针分配给指向整数的指针?
- r - R ggtree:如何使用 ggtree 标记单个树尖,类似于使用 geom_cladelabel 标记节点
- python - 熊猫 txt 文件到数据框
- npm - Windows cmd.exe:“'npm' 不是内部或外部命令、可运行程序或批处理文件。”
- c# - 测试执行sql查询时对象引用错误
- javascript - 在创建嵌套对象时,object.prototype 上的 defineproperty 不会触发
- python - 将图像上传到 Google Drive 共享文件夹,但无法使图像在 Google Drive 中显示为 jpg
- java - java中自定义3d渲染器的问题
- node.js - 使用 ADAL.js 嵌入 Power BI 报表