0

Please, can you help me how can i Get django OAuth2 Toolkit access token from android client like we do it with curl ? I tried many ways in vain for several days. For other information I use retrofit as android http library.

chuseuiti
  • 783
  • 1
  • 9
  • 32
angel
  • 104
  • 12

1 Answers1

0

There are different options to get the token on an Android App from django Oauth2 toolkit, for example you can:

  1. You can create your app as implicit from Oauth toolkit and pass the token from the browser to your android app.

Here you have the explanation about how to register your app as a handler for a URL scheme, this will allow you to go back from the browser to your app:

http://appurl.org/docs/android

(Also in the last link you can see another example of this in slide number 20)

This question explains how to redirect and pass the data from the browser to the phone:

redirecting to Android app from browser

And here you have the workflow between Oauth and Android:

http://www.slideshare.net/briandavidcampbell/is-that-a-token-in-your-phone-in-your-pocket-or-are-you-just-glad-to-see-me-oauth-20-and-mobile-devices

It starts at slide fifteen.

  1. Another option can be to define your App as grant type password and do a request asking for the token:

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(LOGIN_API);
    
    
    // Add your data
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    
    
    nameValuePairs.add(new BasicNameValuePair("username", USERNAME));
    nameValuePairs.add(new BasicNameValuePair("password", PASSWORD));
    nameValuePairs.add(new BasicNameValuePair("grant_type", "password"));
    
    nameValuePairs.add(new BasicNameValuePair("client_id", CLIENT ID))
    nameValuePairs.add(new BasicNameValuePair("client_secrect", CLIENT SECRET));
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    
    // Execute HTTP Post Request
    HttpResponse response = httpclient.execute(httppost);
    

What to use will depend on the use case of your App.

Edited by community:

The HttpClient has been deprecated in the last few years. Here's a substitute code:

        String data = URLEncoder.encode( "grant_type", "UTF-8" ) + "=" + URLEncoder.encode( "password", "UTF-8" );

        data += "&" + URLEncoder.encode( "username", "UTF-8" ) + "=" + URLEncoder.encode( USERNAME, "UTF-8" );

        data += "&" + URLEncoder.encode( "password", "UTF-8" ) + "=" + URLEncoder.encode( PASSWORD, "UTF-8" );

        data += "&" + URLEncoder.encode( "client_id", "UTF-8" ) + "=" + URLEncoder.encode( CLIENT_ID, "UTF-8" );

        data += "&" + URLEncoder.encode( "client_secret", "UTF-8" ) + "=" + URLEncoder.encode( CLIENT_SECRET, "UTF-8" );

        URL server = new URL( param.url );
        HttpURLConnection connection = ( HttpURLConnection ) server.openConnection();
        connection.setDoOutput( true );
        OutputStreamWriter osw = new OutputStreamWriter( connection.getOutputStream() );
        osw.write( data );
        osw.flush();

        int responseCode = connection.getResponseCode();

        if( responseCode == HttpStatus.SC_OK )
        {

            BufferedReader reader = new BufferedReader( new InputStreamReader( connection.getInputStream() ) );
            StringBuffer response = new StringBuffer();
            String line = "";
            while( ( line = reader.readLine() ) != null )
            {
                response.append( line );
            }
            Log.v( "Login", response.toString() );
        }
        else
        {
            Log.v( "CatalogClient", "Response code:" + responseCode );
        }
Community
  • 1
  • 1
chuseuiti
  • 783
  • 1
  • 9
  • 32