[英]invalid_client in jsonresponse in oauth2 android
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. 我正在为Google执行oauth2,我可以获取授权码,但是我无法从授权码中获取访问令牌。
i'm getting invalid json_response like 我正在像无效的json_response
{
"error":"invalid_client"
}
I have tried with by updating the product name of the application. 我尝试通过更新应用程序的产品名称。 by this solution .
通过这种解决方案 。 But it not helped.
但这没有帮助。
Here is the code to obtain the get the AccessToken. 这是获取AccessToken的代码。 GetAccessToken
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;
}}
Code to parse the json_response. 解析json_response的代码。
TokenGet.class 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... 给我,任何建议...
Ok, i had some troubles like you using AsyncTask: 好的,我在使用AsyncTask时遇到了一些麻烦,例如您:
So, i recommend you to use VolleY, its very easy to do what you are doing with this library... 因此,我建议您使用VolleY,使用此库非常容易执行您的操作...
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... 如果不起作用,请通知我...
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. 如果您有兴趣使用Volley,请告诉我...。我不是专家,但是我可以为您提供帮助,实际上我正在使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.