使用api应用程序进行身份验证后,但意图没有数据

时间:2019-04-14 21:49:45

标签: android oauth-2.0 stackexchange-api

我正在尝试使用stackexchange api构建应用程序,并且用户在通过stackoverflow.com对自身进行身份验证后将能够使用该应用程序。但是我得到的问题是重新打开应用程序后完成身份验证后,我正在onResume()中寻找意图的数据,因此它为null,但应包含带有访问令牌的回调URL。当我打开传递给浏览器进行身份验证的URL时,与回调URL关联了access_token,但在应用程序中,我得到的是null!

MainAcitivity

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    Uri uri = new Uri.Builder()
            .scheme("https")
            .authority("stackoverflow.com")
            .appendPath("oauth").appendPath("dialog")
            .appendQueryParameter("client_id", "14910")
            .appendQueryParameter("scope", "read_inbox")
            .appendQueryParameter("redirect_uri", "https://stackoverflow.com/oauth/login_success")
            .build();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.d(TAG, "onCreate: uri : " + uri);
        Intent intent = new Intent(
                Intent.ACTION_VIEW,
                uri);
        startActivity(intent);

    }

    @Override
    protected void onResume() {
        super.onResume();


        Log.d(TAG, "onResume: intent: " + getIntent());

        Log.d(TAG, "onResume: uri: " + getIntent().getData());
        Uri uri = getIntent().getData();
        if (uri != null && uri.toString().startsWith("stackoverflow.com/oauth/login_success")) {

            String code = uri.getQueryParameter("code");
            if (code != null) {
                Log.d(TAG, "onResume: code:" + code);
            } else if (uri.getQueryParameter("error") != null) {

                Log.d(TAG, "onResume: error" + uri.getQueryParameter("error"));
            }

        }else{
            Log.d(TAG, "onResume: else");
        }
    }
}

清单

...
 <activity android:name=".MainActivity"
            android:launchMode="singleTop">
            <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="stackoverflow.com"
                    android:pathPrefix="/oauth/login_success"
                    android:scheme="https" />
            </intent-filter>
        </activity>
...

项目的StackExchange API设置

[项目图片]:https://github.com/chetan882777/git-training/blob/master/Screenshot_2019-04-14%20Editing%20project.png

如果我在这里显示uri传递了意图:

 https://stackoverflow.com/oauth/dialog?client_id=14910&scope=read_inbox&redirect_uri=https%3A%2F%2Fstackoverflow.com%2Foauth%2Flogin_success

,然后在身份验证后获得该响应URL:

https://stackoverflow.com/oauth/login_success#access_token=nwIxeGi73Bf0rA9Ij4iwcQ))&expires=86400

0 个答案:

没有答案