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

时间:2015-02-19 03:13:01

标签: android django oauth-2.0 retrofit oauth2-toolkit

拜托,你能帮我吗?我怎样才能从android客户端获取django OAuth2 Toolkit访问令牌,就像我们用curl一样?我好几天尝试了许多方法都是徒劳的。对于其他信息,我使用改造作为android http库。

1 个答案:

答案 0 :(得分:0)

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

  1. 您可以从Oauth工具包中将您的应用程序创建为 隐式 ,并将该令牌从浏览器传递到您的Android应用程序。
  2. 在这里,您有关于如何将您的应用注册为URL方案的处理程序的说明,这将允许您从浏览器返回到您的应用程序:

    http://appurl.org/docs/android

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

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

    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

    从15号幻灯片开始。

    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);
      
    2. 使用方法取决于您的应用程序的用例。

      社区编辑:

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