Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从android客户端获取django OAuth2 Toolkit访问令牌_Android_Django_Oauth 2.0_Retrofit_Oauth2 Toolkit - Fatal编程技术网

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

从android客户端获取django OAuth2 Toolkit访问令牌,android,django,oauth-2.0,retrofit,oauth2-toolkit,Android,Django,Oauth 2.0,Retrofit,Oauth2 Toolkit,请,您能帮助我如何从android客户端获得django OAuth2 Toolkit访问令牌,就像我们使用curl一样?我尝试了许多方法,但几天都没有成功。对于其他信息,我使用改型作为android http库。从django Oauth2 toolkit获取android应用程序上的令牌有不同的选项,例如,您可以: 您可以从Oauth toolkit以隐式的方式创建应用程序,并将令牌从浏览器传递到android应用程序 这里有关于如何将应用程序注册为URL方案处理程序的说明,这将允许您从浏览

请,您能帮助我如何从android客户端获得django OAuth2 Toolkit访问令牌,就像我们使用curl一样?我尝试了许多方法,但几天都没有成功。对于其他信息,我使用改型作为android http库。

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

  • 您可以从Oauth toolkit以隐式的方式创建应用程序,并将令牌从浏览器传递到android应用程序
  • 这里有关于如何将应用程序注册为URL方案处理程序的说明,这将允许您从浏览器返回到应用程序:

    (同样在最后一个链接中,您可以在第20张幻灯片中看到另一个示例)

    此问题说明如何将数据从浏览器重定向并传递到手机:

    这里是Oauth和Android之间的工作流程:

    从幻灯片15开始

  • 另一个选项是将应用程序定义为授权类型密码,并请求令牌:

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