【问题标题】:invalid_client in jsonresponse in oauth2 androidoauth2 android中jsonresponse中的invalid_client
【发布时间】:2014-10-07 10:01:40
【问题描述】:

我正在为 google 做 oauth2,我能够获取授权码,但我无法从授权码中获取 accesstoken。

我的 json_response 无效

{

"error":"invalid_client"
}   

我尝试过更新应用程序的产品名称。通过这个solution。 但它没有帮助。

这里是获取获取AccessToken的代码。 获取访问令牌

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

给我,任何建议...

【问题讨论】:

    标签: java android json oauth-2.0


    【解决方案1】:

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

    所以,我推荐你使用 VolleY,它很容易用这个库来做你正在做的事情......

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

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

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

    如果您有兴趣使用 Volley,请告诉我....我不是专家,但我可以帮助您,实际上我正在使用它。

    【讨论】:

      猜你喜欢
      • 2013-06-14
      • 2018-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多