oauth2 android中jsonresponse中的invalid

编程入门 行业动态 更新时间:2024-10-21 11:59:16
本文介绍了oauth2 android中jsonresponse中的invalid_client的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在为Google做oauth2,我可以获取授权码,但是我无法从授权码中获取访问令牌.

I'm doing the oauth2 for google i'm able to getting the authorization code but i can't able to get the accesstoken from the authorization code.

我收到无效的json_response之类的

i'm getting invalid json_response like

{ "error":"invalid_client" }

我尝试通过更新应用程序的产品名称进行操作.通过 解决方案 . 但这没有帮助.

I have tried with by updating the product name of the application. by this solution. But it not helped.

这里是获取AccessToken的代码. GetAccessToken

Here is the code to obtain the get the AccessToken. GetAccessToken

public class GetAccessToken { static InputStream is = null; static JSONObject jObj = null; static String json = ""; public GetAccessToken() {} List < NameValuePair > params = new ArrayList < NameValuePair > (); Map < String, String > mapn; DefaultHttpClient httpClient; HttpPost httpPost; public JSONObject gettoken(String address, String token, String client_id, String client_secret, String redirect_uri, String grant_type) { // Making HTTP request try { // DefaultHttpClient httpClient = new DefaultHttpClient(); httpPost = new HttpPost(address); params.add(new BasicNameValuePair("code", token)); params.add(new BasicNameValuePair("client_id", client_id)); params.add(new BasicNameValuePair("client_secret", client_secret)); params.add(new BasicNameValuePair("redirect_uri", redirect_uri)); params.add(new BasicNameValuePair("grant_type", grant_type)); httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded"); httpPost.setEntity(new UrlEncodedFormEntity(params)); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); is = httpEntity.getContent(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { BufferedReader reader = new BufferedReader(new InputStreamReader( is, "iso-8859-1"), 8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); json = sb.toString(); Log.e("JSONStr", json); } catch (Exception e) { e.getMessage(); Log.e("Buffer Error", "Error converting result " + e.toString()); } // Parse the String to a JSON Object try { jObj = new JSONObject(json); } catch (JSONException e) { Log.e("JSON Parser", "Error parsing data " + e.toString()); } // Return JSON String return jObj; }}

用于解析json_response的代码.

Code to parse the json_response.

TokenGet.class

private class TokenGet extends AsyncTask < String, String, JSONObject > { private ProgressDialog pDialog; String Code;@ Override protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(MainActivity.this); pDialog.setMessage("Contacting Google ..."); pDialog.setIndeterminate(false); pDialog.setCancelable(true); Code = pref.getString("Code", ""); pDialog.show(); }@ Override protected JSONObject doInBackground(String...args) { GetAccessToken jParser = new GetAccessToken(); JSONObject json = jParser.gettoken(TOKEN_URL, authCode, CLIENT_ID, CLIENT_SECRET, REDIRECT_URI, GRANT_TYPE); return json; }@ Override protected void onPostExecute(JSONObject json) { pDialog.dismiss(); if (json != null) { try { String tok = json.getString("access_token"); String expire = json.getString("expires_in"); String refresh = json.getString("refresh_token"); Log.d("json", json.toString()); Log.d("Token Access", tok); Log.d("Expire", expire); Log.d("Refresh", refresh); auth.setText("Authenticated"); Access.setText("Access Token:" + tok + "\nExpires:" + expire + "\nRefresh Token:" + refresh); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { Toast.makeText(getApplicationContext(), "Network Error", Toast.LENGTH_SHORT).show(); pDialog.dismiss(); } } }

给我,有任何建议...

Give Me,Any Suggestion...

推荐答案

好,我在使用AsyncTask时遇到了一些麻烦,例如您:

Ok, i had some troubles like you using AsyncTask:

因此,我建议您使用VolleY,使用此库非常容易执行您的操作...

So, i recommend you to use VolleY, its very easy to do what you are doing with this library...

我认为您的问题在于您没有使用正确的字符集...请尝试将其添加到代码中:

I think your problema is that you are not using the correct Charset... try adding this to your code:

httpPost.setHeader("Content-Type","application/x-www-form-urlencoded; charset = utf-8");

httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");

如果它不起作用,请告诉我...

if it doesnt work, lets me know...

如果您有兴趣使用Volley,请让我知道....我不是专家,但是我可以为您提供帮助,实际上我正在使用它.

If you are interested to use Volley lets me know.... i am not an expert, but i can help you, actually i am using this.

更多推荐

oauth2 android中jsonresponse中的invalid

本文发布于:2023-11-12 04:14:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1580487.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:android   invalid   jsonresponse

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!