D
D
Denis Petrenko2020-02-24 16:53:30
API
Denis Petrenko, 2020-02-24 16:53:30

How to sync a file through Google Drive API?

I have an application written in C++ and Qt5. It creates the file I need. How to create "upload/download" to/from Google Driv'a, respectively, taking into account the fact that there is a Google account login/password. You need to create a folder on the disk at the root and upload the file to it.
In which direction to dig?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
SerJook, 2020-02-24
@Termoslik

Well, you should probably start by reading the Google Drive API documentation .
You don't need a username and password.
Authentication is done using OAuth 2.0.
The first thing you need to do is to create an application through the Google API Console , get your application details and connect the Google Drive API to it.
I will describe one of the authentication methods using a confirmation code below.

  1. First, open the URL to the user (in the system browser):
    https://accounts.google.com/o/oauth2/auth?scope={scope}&redirect_uri={redirect_uri}&response_type=code&client_id={client_id}

    (This can be done using the QDesktopServices::openUrl method
    where {scope} = " https://www.googleapis.com/auth/drive "
    {redirect_uri} = "urn:ietf:wg:oauth:2.0:oob"
    {client_id }= blablabla.apps.googleusercontent.com (your app ID)
    The user confirms access and copies the verification code
    You are prompted to enter a verification code in your app The user enters it.
  2. To get an access token, you need to send a POST request to the https://www.googleapis.com/oauth2/v3/token
    server with the following parameters:
    code - confirmation code entered by the user
    client_id - your application ID (blablabla.apps.googleusercontent.com)
    client_secret - secret key of your application
    redirect_uri - the same redirect uri as in the first case
    grant_type = "authorization_code" The
    request can be made using qnetworkaccessmanager
    In response, you get an access_token that will need to be used for requests to the Google Drive API.
    Also in the response there will be a refresh_token, which is needed to update the access_token.
  3. To create a folder, you will need to send a POST request to the URL:
    https://www.googleapis.com/drive/v3/files Write
    in the request headers:
    Authorization:  Bearer <полученный access_token>
    Content-Type: application/json

    Send json in request body:
    {
       "title": "Название папки"
       "mimeType":  "application/vnd.google-apps.folder"
    }

    In response, you will receive json with the folder ID.
    How to download the file is described
Here, in
short, everything is elementary.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question