【发布时间】:2018-12-04 01:52:13
【问题描述】:
我非常纠结于一个我现在花了很多时间和很多不同的试验和错误的问题。
我的应用使用 OAuth 2.0 将图像发送给供应商 (Kiteworks),这是一个安全的存储设施。 Kiteworks 有自己的 API 来实现这一点。
我的应用正在启动他们的登录页面,用户登录并被重定向到其中包含授权代码的回调 URL。
我现在想要实现的是,一旦点击此页面,它就会再次重定向到我的应用程序,但最重要的是它需要授权码,以便我可以使用它来交换令牌以发送图片。
这是我尝试过的代码,它可以让我用我的应用程序打开重定向网址(让我可以选择应用程序或 chrome)。我希望当我选择应用程序时它会存储授权码,但它不是:
清单:
<activity
android:name=".Kiteworks_SignIn"
android:launchMode="singleTop"
android:noHistory="true"
android:screenOrientation="behind"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.LAUNCHER" />
<!-- Accepts URIs that begin with "http://www.example.com/gizmos” -->
<data android:scheme="https"
android:host="sync.almacgroup.com"
android:pathPrefix="/rest/callback">
<!--https://sync.almacgroup.com/oauth/authorize?client_id=1fb7c350-bb6a-5741-86b9-43afc2f1642f&redirect_uri=https://sync.almacgroup.com/rest/callback.html?display%3Dmobile&response_type=code&scope=&m=1&force_login=1-->
</data>
/>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
Kiteworks 签到活动:
browser = findViewById(R.id.buttonBrowser);
browser.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
browserIntent = new Intent(Intent.ACTION_VIEW, uri);
startActivityForResult(browserIntent, authcode);
if (browserIntent != null && browserIntent.getData() != null && "https://sync.almacgroup.com/oauth/authorize?client_id=1fb7c350-bb6a-5741-86b9-43afc2f1642f&redirect_uri=https://sync.almacgroup.com/rest/callback.html?display%3Dmobile&response_type=code&scope=&m=1&force_login=1".equals(browserIntent.getData().getScheme())) {
authcode = Integer.parseInt(getIntent().getData().getQueryParameter("code"));
// complete oauth flow
setResult(Activity.RESULT_OK);
finish();
} else {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://sync.almacgroup.com/oauth/authorize?client_id=1fb7c350-bb6a-5741-86b9-43afc2f1642f&redirect_uri=https://sync.almacgroup.com/rest/callback.html?display%3Dmobile&response_type=code&scope=&m=1&force_login=1"))
.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_FROM_BACKGROUND);
startActivityForResult(browserIntent, authcode);
setResult(Activity.RESULT_OK);
finish();
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
try {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
authcode = Integer.parseInt(getIntent().getData().getQueryParameter("code"));
Toast.makeText(this, authcode, Toast.LENGTH_SHORT).show();
}
} catch (Exception ex) {
Toast.makeText(Kiteworks_SignIn.this, ex.toString(),
Toast.LENGTH_LONG).show();
}
}
有谁知道我哪里出错了或者用其他方法来实现这个?
任何帮助将不胜感激:)
【问题讨论】:
-
@Kling Klang 有什么想法吗?
-
不,抱歉。我以前从未使用过深度链接。
-
您最终找到了可行的解决方案吗?一个客户要求我将我的 php 应用程序连接到 Kitewoks,但我不确定我能否实现它。我在网上找不到示例。
标签: java android oauth-2.0 authorization httprequest