Google Drive Generate Api Key
So as a small assignment, I tasked myself to find out whether it is possible to access and possibly manage my Google drive using REST API calls.
Google has exposed a very sophisticated amount of APIs for its cloud services like Drive, Maps, Calendar, etc. In this post, we will see how to access the Google Drive API and create a dummy file on my Drive using a REST Call.
The process is simple:
When you create your application, you register it using the Google API Console. Google then provides information you'll need later, such as a client ID and a client secret. Activate the Drive API in the Google API Console. (If the API isn't listed in the API Console, then skip this step.). Apr 08, 2020 A link for downloading the content of the file in a browser. This is only available for files with binary content in Google Drive. A link for opening the file in a relevant Google editor or viewer in a browser. A static, unauthenticated link to the file's icon. A short-lived link to the file's thumbnail. Mar 25, 2020 At the bottom of the APIs & Services dashboard, click Google Drive API. If you don’t see this option, you must enable the Google Drive API. In the left-hand nav, click Drive UI Integration. The Configuration page appears. Enter an application name in the Application Name field.
When you create your application, you register it using the Google API Console. Google then provides information you'll need later, such as a client ID and a client secret. Activate the Drive API in the Google API Console. (If the API isn't listed in the API Console, then skip this step.). Where do I find my Google Maps API key? Because of recent changes that Google did, you need your own Google Maps API key if you want to use Google Maps on you own website. But don't worry! It's very easy to get one and it's free - we'll show you how, step by step. Click on the following button to request your own API key (opens in new tab. Apr 10, 2020 To get an API key: Go to the Google Cloud Platform Console. Click the project drop-down and select or create the project for which you want to add an API key. Click the menu button and select APIs & Services Credentials. On the Credentials page, click Create credentials API key. The API key created dialog displays your newly created API key.
- Create a Project in Google API Console and Enable your API
- Create Credentials to access the API
- Obtain an Authorization Code using created Credentials
- Use the Authorization code as Authentication Token in Postman to access Google Drive
Lets get started!
First, we need to log in to Google API Console using a valid Gmail Account.
Why do we need to Sign in to Google services?
Since Google APIs use OAuth2 for Authentication, we cannot simply use Email and Password to access the API using Postman. Therefore, Postman requires a secure Authentication token to access the Google API. In order to get this token, we need to sign in to the Google API console, generate a token which represents the same account and use it in Postman.
Once logged in, you should see API Console Dashboard, on the Menu bar, select 'Select a Project' and click New Project on the dialog. Enter project name and create the project.
Now you should be able to see your Project name on the top of the page.
Go back to dashboard and click 'Enable APIs and Services'
In API Library, search and select Google Drive API and Enable it.
This will enable the drive API on your Project, once enabled, you will be redirected to the Overview page, now we need to create Credentials to access this API. Click 'Create Credentials'.
Enter following details and Press 'What Credentials do I need?'
Create Client for OAuth, provide any name in Name field.
Add a URI for the application from where you will be accessing the API in Authorized redirect URIs field, since we are going to access from Postman, we can add any URL here.
This URI is used for redirection once authorization is done. It should be a subdomain of Authorized Domains list in OAuth Consent tab, you will be prompted to add it if you haven't already. Please make sure that you do.
Once done with these values, click 'Create OAuth client ID'.
Now select your Email address from OAuth Consent screen and enter a product name and press Continue.
Download the JSON which contains information about your client using the Download button.
Click Done. This will create a set of credentials for your API.
Just to make sure, go to OAuth Consent Screen on Credentials tab and check if you have the correct value in Authorized Domain (if its not there, add it):
Edit this URL to include your Client ID and Redirect URL - You can get both of them either from the credentials page or the JSON you downloaded.
https://accounts.google.com/o/oauth2/auth?scope=https://www.googleapis.com/auth/drive&response_type=code&access_type=offline&redirect_uri=<redirect url>&client_id=<client id>
Open a new Incognito window in any browser with Developer tools -> Network tab, paste this URL with updated values and open it.
When prompted, log in with the same Google Account you used earlier and allow access.
You will be taken to the redirect URL you provided earlier. When you're on the redirect URL, notice the value calls in Network tab in Dev tools, and copy the one that looks like below:
Extract the value of 'code' parameter from this request and keep it in notepad or any text editor.
Now, Open Postman and open a new Tab, select POST as request type with URL set to: https://accounts.google.com/o/oauth2/token
Set Body to x-www-form-urlencoded and provide following values using your JSON or Credential manger in Console:
So, now we have the Access Token for our API. We can now access all endpoints that Google Drive API offers, for a list of these endpoints, check out the Google Drive API Refence
We're going to try and Get the list of files in my Google Drive using Postman to see if our Token works:
- Open a new GET Tab in Postman
- In Headers, provide Keys and Values like shown in screenshot below.
- Value of Authorization Key contains 'Bearer <your auth token>'
This should return the list of all files in your Google Drive.
Similarly, you can perform a lot of operations on your Google Drive using the same Authentication Token.
To use the Maps Embed API you must have an API key. The API key is a unique identifier that isused to authenticate requests associated with your project for usage and billing purposes.
Get the API key
You must have at least one API key associated with your project.
To get an API key:
- Visit the Google Cloud Platform Console.
- Click the project drop-down and select or create the project for which you want to add an API key.
- Click the menu button and select APIs & Services > Credentials.
- On the Credentials page, click Create credentials > API key.
The API key created dialog displays your newly created API key. - Click Close.
The new API key is listed on the Credentials page under API keys.
(Remember to restrict the API key before using it in production.)
Add the API key to your request
You must include an API key with every Maps Embed API request.In the following example, replace YOUR_API_KEY
withyour API key.
For more information about MODE
and parameters
options in the code above, see Forming the URL for the Maps Embed API.
Restrict the API key
We strongly recommend that you restrict your API key. Restrictions provide added security and helpensure only authorized requests are made with your API key. There are two restrictions. You shouldset both:
- Application restriction: Limits usage of the API key to either websites (HTTP referrers), web servers (IP addresses), or mobile apps (Android apps or iOS apps). You can select only one restriction from this category, based on the platform of the API or SDK (see GMP APIs by Platform).
Note: If you need to call web, web service, and/or mobile APIs from the same (client-side) app, create and restrict multiple keys.
- API restriction: Limits usage of the API key to one or more APIs or SDKs. Requests to an API or SDK associated with the API key will be processed. Requests to an API or SDK not associated with the API key will fail. (The API or SDK must be enabled and must support the application restriction.)
To restrict an API key:
Google Drive Generate Api Key Online
- Go to the Google Cloud Platform Console.
- Click the project drop-down select the project that contains the API key you want to secure.
- Click the menu button and select APIs & Services > Credentials.
- On the Credentials page, click the name of the API key that you want to secure.
- On the Restrict and rename API key page, set the restrictions:
- Application restrictions
- Select HTTP referrers (web sites).
- Add the referrers.
- API restrictions
- Select Restrict key.
- Click Select APIs and select Maps Embed API.
(If the Maps Embed API is not listed, you need to enable it.) - Click SAVE.