Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Intent.ACTION_VIEW Basic Authentication

How do I pass along HTTP Basic Authentication information to Intent.ACTION_VIEW? Here's where I'm firing the intent:

public class OutageListFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> {

    // ...

    @Override
    public void onListItemClick(ListView listView, View view, int position, long id) {
        super.onListItemClick(listView, view, position, id);

        // Get a URI for the selected item, then start an Activity that displays the URI. Any
        // Activity that filters for ACTION_VIEW and a URI can accept this. In most cases, this will
        // be a browser.
        String outageUrlString = "http://demo:[email protected]/opennms/outage/detail.htm?id=204042";
        Log.i(TAG, "Opening URL: " + outageUrlString);
        // Get a Uri object for the URL string
        Uri outageURI = Uri.parse(outageUrlString);
        Intent i = new Intent(Intent.ACTION_VIEW, outageURI);
        startActivity(i)
    }

}

I have also tried Uri.fromParts(), same deal. Curl works just fine.

like image 505
Wrolf Avatar asked Mar 18 '23 11:03

Wrolf


1 Answers

Turns out you can add HTTP headers to the Intent via a Bundle, and specifically add an Authorization header with a Base64 encoded user id.

    Intent i = new Intent(Intent.ACTION_VIEW, outageURI);

    String authorization = user + ":" + password;
    String authorizationBase64 = Base64.encodeToString(authorization.getBytes(), 0);

    Bundle bundle = new Bundle();
    bundle.putString("Authorization", "Basic " + authorizationBase64);
    i.putExtra(Browser.EXTRA_HEADERS, bundle);
    Log.d(TAG, "intent:" + i.toString());

    startActivity(i);
like image 118
Wrolf Avatar answered Mar 27 '23 18:03

Wrolf