从 android 客户端获取 django OAuth2 Toolkit 访问令牌

Get django OAuth2 Toolkit access token from android client

拜托,你能帮助我如何像我们使用 curl 那样从 android 客户端获取 django OAuth2 Toolkit 访问令牌?连续几天试了很多方法都没用。对于其他信息,我将改造用作 android http 库。

从 django Oauth2 工具包获取 Android 应用程序上的令牌有不同的选项,例如您可以:

  1. 您可以通过 Oauth 工具包将您的应用程序创建为 implicit 并将令牌从浏览器传递到您的 android 应用程序.

这里有关于如何将您的应用程序注册为 URL 方案的处理程序的说明,这将允许您从浏览器返回到您的应用程序:

http://appurl.org/docs/android

(同样在最后一个 link 中,您可以在幻灯片 20 中看到另一个例子)

这个问题解释了如何将数据从浏览器重定向和传递到 phone:

redirecting to Android app from browser

这里是 Oauth 和 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

从第十五张幻灯片开始。

  1. 另一种选择是将您的应用程序定义为授予类型 密码 并请求令牌:

    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);
    

使用什么取决于您的应用程序的用例。

社区编辑:

HttpClient 在过去几年中已被弃用。这是一个替代代码:

        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 );
        }