The other day, when I used Google's API, I overlooked a Not Found error and a Bad Request error, so I will summarize it as a memo.
https://console.developers.google.com/apis/dashboard
Select this if the user consents to authorization from the screen
After entering the application type, name, and approved redirect URI, a client ID and client secret were generated.
Get an access token in JSON format behind the scenes without screen operation. Select this for batch processing
Download the file in JSON format
Set client and scoop and access from URL
https://accounts.google.com/o/oauth2/v2/auth?
scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.metadata.readonly&
access_type=offline&
include_granted_scopes=true&
redirect_uri=http%3A%2F%2localhost:8080&
response_type=code&
client_id=Client ID obtained above
If you agree with Google, you can get the authorization code from the URL.
http://localhost:8080/?code=xxxxxxxxxxxx&scope=yyyyyyyyyyy
Point 1: POST method. NG with GET method Point 2: Set "Content-Type: application / x-www-form-urlencoded" in the header. NG if not set or JSON is set.
Test.java
String url = "https://www.googleapis.com/oauth2/v4/token";
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost request = new HttpPost(url);
List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>();
parameters.add(new BasicNameValuePair("code", authorizationCode)); //The authorization code obtained above
parameters.add(new BasicNameValuePair("client_id", oAuthClientId)); //Client ID for API credentials on the console
parameters.add(new BasicNameValuePair("client_secret", oAuthClientSecret)); //Client secret of API credentials on the console
parameters.add(new BasicNameValuePair("redirect_uri", "http://localhost:8080")); //Approved redirect URI that you have set
parameters.add(new BasicNameValuePair("grant_type", "authorization_code")); //Fixed
HttpEntity entity = new UrlEncodedFormEntity(parameters);
request.setEntity(entity);
request.setHeader("Content-Type", "application/x-www-form-urlencoded");
CloseableHttpResponse response = httpclient.execute(request);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String result = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println(result);
}
response.close();
Response field:
Now that you have the access token, you can get the data from Google.
This is an example of setting an access token in the header.
curl -H "Authorization: Bearer <access_token>" https://www.googleapis.com/drive/v2/files
The access token has an expiration date. After a certain period (60 minutes), it becomes invalid. At this time, you may get the authorization code again, It is common to reissue an access token using a refresh token because it is troublesome for the user.
It's the same as an access token.
Test.java
String url = "https://www.googleapis.com/oauth2/v4/token";
HttpPost request = new HttpPost(url);
List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>();
parameters.add(new BasicNameValuePair("refresh_token", "xxxxxxx")); //Refresh token you have
parameters.add(new BasicNameValuePair("client_id", oAuthClientId)); //Client ID for API credentials on the console
parameters.add(new BasicNameValuePair("client_secret", oAuthClientSecret)); //Client secret of API credentials on the console
parameters.add(new BasicNameValuePair("redirect_uri", "http://localhost:8080")); //Approved redirect URI that you have set
parameters.add(new BasicNameValuePair("grant_type", "refresh_token")); //Fixed
HttpEntity entity = new UrlEncodedFormEntity(parameters);
request.setEntity(entity);
request.setHeader("Content-Type", "application/x-www-form-urlencoded");
CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse response = httpclient.execute(request);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String result = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println(result);
}
response.close();
This is an example response.
As shown in the sample, there is no refresh token, so if the refresh token is also invalid, you will have to start over from obtaining the authorization code.
Reference URL: https://developers.google.com/identity/protocols/OAuth2WebServer
that's all
Recommended Posts