Google Oauth 2.0 authentication for limited input device not working except on curl

主宰稳场 提交于 2019-12-12 04:12:47

问题


I am trying to use a custom java application of mine to upload videos to my youtube account via an access limited device like a Raspberry pi running as a server.

For this I am using the Google Oauth 2.0 for limited input device as a reference.

I followed the steps mentioned with my custom java application, Fiddler and curl, the surprise is as follows:

  1. All of the calls worked right as mentioned by Google Oauth 2.0 for limited input device for curl.

  2. But issues were observed with Fiddler and my custom java app for the following call:

When I am trying to get the access token from Google server (Step 4 from Google Oauth link) by posting similar request:

POST /o/oauth2/token HTTP/1.1    
Host: accounts.google.com
Content-Type: application/x-www-form-urlencoded

client_id=1084945748469-eg34imk572gdhu83gj5p0an9fut6urp5.apps.googleusercontent.com&
client_secret=hDBmMRhz7eJRsM9Z2q1oFBSem&
code=4/YMSlR3fSCC1NtUh073DuZKTJJ3ss&
grant_type=http://oauth.net/grant_type/device/1.0

but instead of getting the 'access_token' as response I am getting the following :

Status Code:400 Response: {   "error" : "invalid_request", 
"error_description" : "Required parameter is missing: grant_type" }

Note : With or without url encoding, my problem stays the same.

I am unable to understand what the issue is with my custom java app or with fiddler, Please help.

Following are my fiddler requests:

(One can get oauth credentials (client_id and client_secret) by following this)

Fiddler request: (url encoded, obscured client secret)

POST HTTP/1.1
https://accounts.google.com/o/oauth2/token?client_id=308065994473-ur9dd7003ajs6mvr5s4kqnugr6j8tsf2.apps.googleusercontent.com&client_secret=XXXXXXXXXXXXXXX&code=4%2FWR-qiTquqB0e4-0LCy0-7rZ2kkE2&grant_type=http%3A%2F%2Foauth.net%2Fgrant_type%2Fdevice%2F1.0

Content-Type: application/x-www-form-urlencoded

(non url encoded, obscured client secret)

POST HTTP/1.1
https://accounts.google.com/o/oauth2/token?client_id=308065994473-ur9dd7003ajs6mvr5s4kqnugr6j8tsf2.apps.googleusercontent.com&client_secret=XXXXXXXXXXXXXX&code=4/WR-qiTquqB0e4-0LCy0-7rZ2kkE2&grant_type=http://oauth.net/grant_type/device/1.0

Java code project is available at (maven project, check the test case for the Oauth calls):

https://docs.google.com/file/d/0B8ltWBtPF-DVMDZFNHNMZXpCQlk


回答1:


The parameters need to be added in the http post request body not in the url, Google documentation is confusing on this part.

    public synchronized HttpResponse executePOST(HttpEntity httpEntity, String path) throws IOException {
    if (!parameters.isEmpty()) {
        httpPost.setEntity(new UrlEncodedFormEntity(parameters));
    }

    httpPost = new HttpPost(path);

    logger.info(target.toHostString());
    logger.info(httpPost.getURI().toString());
    logger.info(httpPost.getRequestLine().toString());
    for (Header header : headers) {
        logger.info(header.getName() + ": " + header.getValue());
        httpPost.addHeader(header);
    }
    httpResponse = httpClient.execute(target, httpPost);
    return httpResponse;
}


来源:https://stackoverflow.com/questions/24380895/google-oauth-2-0-authentication-for-limited-input-device-not-working-except-on-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!