Google Auth2 giving “invalid_request” response for getting access token & refresh token

荒凉一梦 提交于 2019-12-01 00:00:01

After a chat we found what the problems where. The payload wrong and the content-type was set wrong. The following code is the solution:

    HttpClient client = new DefaultHttpClient();
    HttpPost request = new HttpPost("https://accounts.google.com/o/oauth2/token" );
    request.setHeader("Content-type", "application/x-www-form-urlencoded");

    //Please make this custom with you're credentials
    String requestBody = "code=123123132&client_secret=eufFgyAZ8Rmjsk1MaADYsHYW&redirect_uri=urn:ietf:wg:oauth:2.0:oob&grant_type=authorization_code&client_id=128169428269.apps.googleusercontent.com";

    try {
        request.setEntity(new StringEntity(requestBody));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }

    /* Checking response */
    try {
        HttpResponse response = client.execute(request);
        String results = "ERROR";
        results = EntityUtils.toString(response.getEntity());
        Log.d("STACK", "Response::" + results);
    } catch (IOException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!