1

I'm trying to build an app using stackexchange api and user will able to use app after authenticating them self with stackoverflow.com . But problem here i am getting is after completing authentication when app is reopened and i am looking for data in intent in onResume() so it is null but is should consist a callback url with access token. when i open url which i passed to intent for authentication into browser so with callback url access_token is associated but in app i am getting 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");
        }
    }
}

Manifest

...
 <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 setup for project

[project image]: https://github.com/chetan882777/git-training/blob/master/Screenshot_2019-04-14%20Editing%20project.png "

If i show here uri passed into intent:

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

and after that response url i obtained after authentication:

https://stackoverflow.com/oauth/login_success#access_token=nwIxeGi73Bf0rA9Ij4iwcQ))&expires=86400
Community
  • 1
  • 1
Chetan Pawar
  • 404
  • 3
  • 4
  • I don't code much Android. However, the question code is using `getQueryParameter()` but the SE API sends the token in the hash(fragment). This means you should be using something like `getFragment() ` to retrieve it. – Brock Adams Apr 15 '19 at 02:10
  • 1
    OK i will change it, but problem here is `getIntent().getData()` is returning null so code will never reach to `getQueryParameter()`. – Chetan Pawar Apr 15 '19 at 05:57

0 Answers0