深度链接检索授权码

时间:2018-06-25 13:09:36

标签: java android oauth-2.0 authorization httprequest

我非常困惑这个问题,我现在已经花了很多时间,并且经历了许多不同的尝试和错误。

我的应用程序正在使用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();
    }
}

有人知道我要去哪里出错了,或者有其他方法可以做到这一点?

任何帮助将不胜感激:)

1 个答案:

答案 0 :(得分:0)

您的查询参数是 var treeGridView = new TreeGridView { BackgroundColor = Colors.White }; treeGridView.Columns.Add(new GridColumn { HeaderText = "Content Type", DataCell = new TextBoxCell(0) }); treeGridView.Columns.Add(new GridColumn { HeaderText = "Create", DataCell = new CustomCell { CreateCell = r => { TreeGridItem item = r.Item as TreeGridItem; ContentTypeTag tag = (ContentTypeTag)item.Tag; var contentType = _siteManager.CurrentSite.ContentTypes.First(x => x.Name.Equals(tag.ClassName)); void Click(object btnSender, EventArgs btnArgs) { //Your Event } var button = new LinkButton { Style = "primary-link-btn", Text = $"Create {contentType.Name.ToSentenceCase()}", Command = new Command(Click) }; return button; } } }); treeGridView.Columns.Add(new GridColumn { HeaderText = "Show All", DataCell = new CustomCell { CreateCell = r => { TreeGridItem item = r.Item as TreeGridItem; ContentTypeTag tag = (ContentTypeTag)item.Tag; var contentType = _siteManager.CurrentSite.ContentTypes.First(x => x.Name.Equals(tag.ClassName)); void Click(object btnSender, EventArgs btnArgs) { //Your Event } var button = new LinkButton { Style = "primary-link-btn", Text = $"Show All {contentType.Name.ToSentenceCase()}", Command = new Command(Click) }; return button; } } }); var treeGridItemCollection = new TreeGridItemCollection(); foreach (var contentType in _siteManager.CurrentSite.ContentTypes) { var item = new TreeGridItem { Values = new string[] { contentType.Name.ToSentenceCase(), "Create", "Show All" }, Tag = new ContentTypeTag { ClassName = contentType.Name } }; treeGridItemCollection.Add(item); } treeGridView.DataStore = treeGridItemCollection; 而不是代码。解析redirect_uri之后,您需要解析名为code的参数。