[英]invalid_client in jsonresponse in oauth2 android
我正在為Google執行oauth2,我可以獲取授權碼,但是我無法從授權碼中獲取訪問令牌。
我正在像無效的json_response
{
"error":"invalid_client"
}
我嘗試通過更新應用程序的產品名稱。 通過這種解決方案 。 但這沒有幫助。
這是獲取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的代碼。
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();
}
}
}
給我,任何建議...
好的,我在使用AsyncTask時遇到了一些麻煩,例如您:
因此,我建議您使用VolleY,使用此庫非常容易執行您的操作...
我認為您的問題是您沒有使用正確的字符集...請嘗試將其添加到代碼中:
httpPost.setHeader(“ Content-Type”,“ application / x-www-form-urlencoded; charset = utf-8”);
如果不起作用,請通知我...
如果您有興趣使用Volley,請告訴我...。我不是專家,但是我可以為您提供幫助,實際上我正在使用它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.