How to Call Google Drive API using curl command with C Programming?

Vanessa Leung
codeburst
Published in
3 min readJun 8, 2019

--

Google provides Google API Client Libraries in popular languages. Though Google also provides basic guidelines for HTTP/REST approach, it’s still a little bit hard for newbies in C programming like me to follow.

In this article, I am going to share my experience of using libcurl to call Google Drive API in C programming. I will also briefly introduce some function of the libcurl library.

Reference

We are following this Google tutorial for most parts of this article.

Using OAuth 2.0 for Web Server Applications

Prerequisite

  1. Follow curl’s website to download & install libcurl if you haven’t done so.

2. Enable APIs for your project

2.1 Open the Library page in the API Console.

2.2 Select the project associated with your application. Create a project if you do not have one already.

2.3 Use the Library page to find Google Drive API that we will use in this tutorial. Click Enable.

Enable Google Drive API for your project

3. Create authorization credentials

3.1 Open the Credentials page in the API Console.

3.2 Click Create credentials > OAuth client ID.

3.3 Complete the form. Set the application type to Other.

Create OAuth Client ID

Step 1: Request device and user codes

In this step, we send an HTTP POST request to Google’s authorization server, athttps://accounts.google.com/o/oauth2/device/code, that identifies your application as well as the access scopes that your application wants to access on the user's behalf.

  1. First we need to write a callback function to download data into a chunk of memory instead of storing it in a file.

I heavily used comments to help you better understand the codes

WriteMemoryCallback function

2. Then we set up the program environment libcurl needs

int main(void)

3. We can now implement the curl example provided by Google:

curl -d "client_id=client_id&scope=https://www.googleapis.com/auth/drive.file" https://accounts.google.com/o/oauth2/device/code

We want to call Google Drive API and manage Google Drive files and folders, so our scope is https://www.googleapis.com/auth/drive.file.

If you want to call other Google APIs, see OAuth 2.0 Scopes for Google APIs for the corresponding scope.

Inside int main(void) function, declare your client id & client secret first, so we could easily call it later.

Declare client id & client secret
Step 1: Request device and user codes

Step 2: Handle the authorization server response

If the Step 1 request is valid, your response will be a JSON object like below:

Sample response

We need to get the device and user code from Step 1’s reponse

Step 2: Handle the authorization server response

Step 3: Display the user code

Display the verification_url and user_code obtained in Step 2 to the user. We need to instruct the user to navigate to the verification_url on a separate device and enter the user_code.

Step 3: Display the user code

Step 4: Poll Google’s authorization server

Since the user will be using a separate device to navigate to the verification_url and grant (or deny) access, the requesting device is not automatically notified when the user responds to the access request. For that reason, we need to poll Google's authorization server to determine when the user has responded to the request.

curl example:

curl -d "client_id=<client_id>&client_secret=<client_secret>& \
code=<device_code>&grant_type=http://oauth.net/grant_type/device/1.0" \
-H "Content-Type: application/x-www-form-urlencoded" \
https://www.googleapis.com/oauth2/v4/token
Step 4: Poll Google’s authorization server

Part 5: User responds to access request

The following image shows a page similar to what users see when they navigate to the verification_url that we displayed in Step 3:

https://www.google.com/device

Step 6: Handle responses to polling requests

Sample response from Step 4:

We need to extract the access_token from the response

Step 7: Calling Google Drive API

We can now use the token to make calls to a Google API.

curl example:

curl https://www.googleapis.com/drive/v2/files?access_token=<access_token>

Add below lines inside the else{} scope

Step 7: Calling Google Drive API

Now we’re done. You could continue trying Google Drive API by uploading & downloading files.

The whole project code

--

--