Step 1. Create a Client ID
See also Retrieve and Use OAuth 2.0 Credentials.- Go to https://console.developers.google.com/
- Switch to appropriate Google account
- Click on ENABLE APIS in the "Enable Google APIs for use in your apps
- Named the project "Upload Project" and agreed to the terms
- Click Drive API and hit Enable API
- Click Credentials in left nav
- Click Create new Client ID
- Select Web Application
- In the form for Consent Screen, name the product "Upload Project"
- Click Save
- Click Create Client ID
- You will get "Client ID", "Client secret", "Redirect URIs" and "JavaScript origins"
Note: We will used ${clientID} to represent the client ID in this post.
Step 2. Generate a new Auth Token
For protocol information about OAuth protocol and workflow, see https://developers.google.com/identity/protocols/OAuth2?csw=1
For REST endpoint information on the oauth2 REST API used here, see https://developers.google.com/identity/protocols/OAuth2WebServer
For parameter information on Auth Token scopes for Google Drive API access, see https://developers.google.com/drive/v2/reference/files/insert
For parameter information on Auth Token scopes for You Tube API access, see https://developers.google.com/youtube/v3/guides/auth/client-side-web-apps
- In developer console, edit settings and change Redirect URIs to http://localhost
- The URL needed to generate the Auth Token looks something like this:
- https://accounts.google.com/o/oauth2/auth?client_id=****nko08rm1s2qebr300ll3a0kakm3aue.apps.googleusercontent.com&redirect_uri=http://localhost&scope=https://www.googleapis.com/auth/drive&response_type=token
- Open Google Chrome and enter this is the browser, after replacing ${clientID}
- https://accounts.google.com/o/oauth2/auth?client_id=${clientID}&redirect_uri=http://localhost&scope=https://www.googleapis.com/auth/drive&response_type=token
- Go through the flows to sign in (Username, Password, User Consent)
- The URL in the Browser will contain the Google API access token which will be something like this:
- http://localhost/#access_token=ya29.xwHjsm--zQ6Wi5qD6mAoMfnDcNdCl0FvaUN8TuAwCvMJ4-YYq2ozj1vKCE1aAGECW4s8&token_type=Bearer&expires_in=3600
- The access token would then be something like this:
- ya29.xwHjsm--zQ6Wi5qD6mAoMfnDcNdCl0FvaUN8TuAwCvMJ4-YYq2ozj1vKCE1aAGECW4s8
Note: In this post, we will refer to this Auth Token using ${AuthToken}
Step 3. List all files (curl)
Open Git Bash command line and execute the following. This will create a file filesResponse.txt with all the files in google drive.
curl -k -H "Authorization: Bearer ${AuthToken}" https://www.googleapis.com/drive/v2/files > filesResponse.txt
curl -k -H "Authorization: Bearer ya29.xwE3y4ErpmdEXVS3v02hC-DEm2gz05ZwpjOvrmdOXOcCSz0YALOH9A1Qc4YHQGnFPqjN" https://www.googleapis.com/drive/v2/files > filesResponse.txt
You will get a json response with a ton of information.
Alternate Step 3: List all files (Java)
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
// Step 1 - LIST ALL FILES TO TEST GOOGLE DRIVE API
// curl -k -H "Authorization: Bearer ya29.xwE3y4ErpmdEXVS3v02hC-DEm2gz05ZwpjOvrmdOXOcCSz0YALOH9A1Qc4YHQGnFPqjN" https://www.googleapis.com/drive/v2/files > files.txt
//<!-- Shipping with selenium-htmlunit-driver 2.41.0 -->
//<!-- Includes httpclient 4.3.1 org.apache.http.client.methods.CloseableHttpResponse -->
//<!-- Includes httpmime 4.3.1 org.apache.http.entity.mime -->
//<!-- Includes commons-lang3 3.1 org.apache.commons.lang3.exception.ExceptionUtils -->
//<dependency>
// <groupId>net.sourceforge.htmlunit</groupId>
// <artifactId>htmlunit</artifactId>
// <version>2.13</version>
//</dependency>
public class ListAllFiles {
public static void main(String[] args) {
try {
String token = "ya29.yAFLSqN1xaR9EKuMrcJu6iLMt9PyaRqotTK01PbaPjd7gZZMtqE9XgFgHz3A5IEwKV7-";
HttpGet httpGet = new HttpGet("https://www.googleapis.com/drive/v2/files");
httpGet.addHeader("Authorization", "Bearer "+token);
CloseableHttpResponse response = new DefaultHttpClient().execute(httpGet);
String jsonResponse = EntityUtils.toString(response.getEntity());
response.close();
System.out.println("jsonResponse:"+jsonResponse);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
// Step 1 - LIST ALL FILES TO TEST GOOGLE DRIVE API
// curl -k -H "Authorization: Bearer ya29.xwE3y4ErpmdEXVS3v02hC-DEm2gz05ZwpjOvrmdOXOcCSz0YALOH9A1Qc4YHQGnFPqjN" https://www.googleapis.com/drive/v2/files > files.txt
//<!-- Shipping with selenium-htmlunit-driver 2.41.0 -->
//<!-- Includes httpclient 4.3.1 org.apache.http.client.methods.CloseableHttpResponse -->
//<!-- Includes httpmime 4.3.1 org.apache.http.entity.mime -->
//<!-- Includes commons-lang3 3.1 org.apache.commons.lang3.exception.ExceptionUtils -->
//<dependency>
// <groupId>net.sourceforge.htmlunit</groupId>
// <artifactId>htmlunit</artifactId>
// <version>2.13</version>
//</dependency>
public class ListAllFiles {
public static void main(String[] args) {
try {
String token = "ya29.yAFLSqN1xaR9EKuMrcJu6iLMt9PyaRqotTK01PbaPjd7gZZMtqE9XgFgHz3A5IEwKV7-";
HttpGet httpGet = new HttpGet("https://www.googleapis.com/drive/v2/files");
httpGet.addHeader("Authorization", "Bearer "+token);
CloseableHttpResponse response = new DefaultHttpClient().execute(httpGet);
String jsonResponse = EntityUtils.toString(response.getEntity());
response.close();
System.out.println("jsonResponse:"+jsonResponse);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
No comments:
Post a Comment