简体   繁体   English

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

[英]Get django OAuth2 Toolkit access token from android client

Please, can you help me how can i Get django OAuth2 Toolkit access token from android client like we do it with curl ? 拜托,你能帮我吗?我怎样才能从android客户端获取django OAuth2 Toolkit访问令牌,就像我们用curl一样? I tried many ways in vain for several days. 我好几天尝试了许多方法都是徒劳的。 For other information I use retrofit as android http library. 对于其他信息,我使用retrofit作为android http库。

There are different options to get the token on an Android App from django Oauth2 toolkit, for example you can: 从django Oauth2工具包获取Android应用程序上的令牌有不同的选项,例如,您可以:

  1. You can create your app as implicit from Oauth toolkit and pass the token from the browser to your android app. 您可以从Oauth工具包隐式创建应用程序,并将令牌从浏览器传递到您的Android应用程序。

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: 在这里,您有关于如何将应用程序注册为URL方案的处理程序的说明,这将允许您从浏览器返回到您的应用程序:

http://appurl.org/docs/android http://appurl.org/docs/android

(Also in the last link you can see another example of this in slide number 20) (同样在最后一个链接中,您可以在幻灯片编号20中看到另一个示例)

This question explains how to redirect and pass the data from the browser to the phone: 此问题解释了如何将数据从浏览器重定向并传递到手机:

redirecting to Android app from browser 从浏览器重定向到Android应用

And here you have the workflow between Oauth and Android: 在这里你有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 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 -和-移动设备

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. HttpClient在过去几年中已被弃用。 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 );
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM