Android中的Java OAuth身份验证与改进2(7.1.1 API 25)

Android中的Java OAuth身份验证与改进2(7.1.1 API 25),android,android-intent,oauth-2.0,retrofit,retrofit2,Android,Android Intent,Oauth 2.0,Retrofit,Retrofit2,在Android(Android 7.1.1 API 25)中,我试图通过使用下面代码片段1中所示的类中的Reformation2库来执行OAuth2身份验证。在onCreate方法中,我启动一个意图以发送授权请求,在onResume方法中,我试图捕获授权代码,如果一切正常,OAuth服务器应该返回授权代码。为了捕获授权代码,我在android清单文件中指定了一个意图过滤器,如下面的代码片段2所示 <uses-permission android:name="android.permiss

在Android(Android 7.1.1 API 25)中,我试图通过使用下面代码片段1中所示的类中的Reformation2库来执行OAuth2身份验证。在onCreate方法中,我启动一个意图以发送授权请求,在onResume方法中,我试图捕获授权代码,如果一切正常,OAuth服务器应该返回授权代码。为了捕获授权代码,我在android清单文件中指定了一个意图过滤器,如下面的代码片段2所示

<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <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="mycallback"
                android:scheme="myauth"/>
        </intent-filter>
    </activity>
</application>
在代码段1中的onResume方法中,从getIntent().getData()调用返回的uri对象始终返回null。因此,执行永远不会进入下面的if块,我无法检索授权代码。考虑到片段1和2中提供的内容,您能告诉我这里缺少什么吗?谢谢

<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <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="mycallback"
                android:scheme="myauth"/>
        </intent-filter>
    </activity>
</application>
片段1

package com.xyz.sdk;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends AppCompatActivity {
private final static String CLIENT_ID = "client";
private final static String CLIENT_SECRET = "secret";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    StringBuilder authURLBuilder = new StringBuilder();

    authURLBuilder.append("https://id.xyz.com/api/connect/authorize").append("?").
            append("grant_type=").append("password").
            append("&username=").append("86110").
            append("&client_id=").append(CLIENT_ID).
            append("&scope=").append("transfers public accounts offline_access").
            append("&response_type=code&redirect_uri=").
            append("myauth://mycallback");

    /*BEGIN: THIS IS WHERE I AM SENDING AUTHORIZATION REQUEST */
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(authURLBuilder.toString()));
    intent.setPackage(this.getClass().getPackage().getName());
    startService(intent);
    /*END: THIS IS WHERE I AM SENDING AUTHORIZATION REQUEST */
}

@Override
protected void onResume(){
    super.onResume();
    Uri uri = getIntent().getData(); //THIS ALWAYS RETURN NULL
    if(uri != null && uri.toString().startsWith(("myauth://mycallback"))) {
        String code = uri.getQueryParameter("code");
        //this.onAuthCodeResponseReceived(code);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}
<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <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="mycallback"
                android:scheme="myauth"/>
        </intent-filter>
    </activity>
</application>
片段2

<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <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="mycallback"
                android:scheme="myauth"/>
        </intent-filter>
    </activity>
</application>


到目前为止,您基本上已经走上正轨,但您可以通过添加一个单独的活动来改进代码处理,我们称之为LoginActivity,并设置
android:launchMode=“singleTop”
(我将很快解释原因),如下所示:

<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <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="mycallback"
                android:scheme="myauth"/>
        </intent-filter>
    </activity>
</application>
<activity android:name=".view.LoginActivity"
        android:theme="@style/AppTheme.NoActionBar"
        android:exported="true"
        android:launchMode="singleTop" >
        <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="mycallback"
                android:scheme="myauth" />
        </intent-filter>
    </activity>
你可能想知道为什么我建议使用onNewIntent而不是onResume,当我开始使用oauth回调时,我遇到了一个类似的问题,除了Chrome之外的一些浏览器会失败调用响应我回调的活动,而没有提供任何意图中的数据,由于android系统使用默认启动模式调用现有活动

<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <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="mycallback"
                android:scheme="myauth"/>
        </intent-filter>
    </activity>
</application>
使用singleTop将确保现有的活动不会以新的意图重新创建,而是以新的意图被带到前台,因此onNewIntent()我希望我已经很好地解释了这一点,并期待您的回复

<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <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="mycallback"
                android:scheme="myauth"/>
        </intent-filter>
    </activity>
</application>

还为特殊情况添加了onCreate中的checkNewIntent,例如,如果您的应用程序因任何原因在后台被杀死,如果用户授予您的应用程序权限,则如果活动被破坏,则可能不会调用onNewIntent,至少从我的经验来看,我相信有人可能会对此有更好的解释:)

仍然没有人有线索?很抱歉,但我觉得下面给出的答案并不能完全回答我原来的问题。。。无论发生什么情况,从getIntent().getData()调用返回的uri对象总是空的。是的,它对我有效,在过去的24小时里,我一直试图找出问题所在,为什么我会变为空,这个人已经解决了你的答案。我是android开发新手,我想知道如何将这个登录活动与我的正常Main活动关联起来。运行时是否会将它们绑定在一起?您可以只使用一个简单的按钮标记为login,在该按钮中,您只需使用一个意图来启动该活动,在登录成功后,您将关闭登录活动,使您的主要活动转到onResume()上,然后检查用户是否登录:请参阅。请让我知道我提供的答案是否对您有效。您能提供完整的LoginActivity类源代码吗?当我使用Android Studio创建登录活动时,它生成的代码与您提供的完全不同。getPresenter().isAuthenticated()我们需要什么来编译这段代码?我将通知LoginActivity的完整类代码。我添加的其他锅炉板代码将不会生成,我只是添加了该代码,以提示您如何检查用户是否登录,我已编辑了该代码,请注意,您必须重写onNewIntent方法
<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <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="mycallback"
                android:scheme="myauth"/>
        </intent-filter>
    </activity>
</application>