Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I can't see the files and folders created via code in my Google Drive

I'm a web developer and i'd like to access to the Google APIs from my business software, I've created a Drive account and I'd to create a folders and files via code (VB.NET). I'm following the documentation, I've created a JWT (Json Web Token), I've used a Service Account and I've obtained an Access Token.

Now I'd like to access to the Google APIs. Using a console application, I created some folders, and I can get a list of created folders via code. But I cannot see those folders in my Google Drive account. How is it possible?

Here is the request I send to the server in order to show the folders and files list: https://www.googleapis.com/files/{FILE_ID}?key={API_KEY}

I obtain a JSON string as response, with the informations relating the FILE_ID elements. e.g.:

{ "kind":"drive#file", "id":"FILE_ID", "mimeType":"application/vnd.google-apps.folder"(if is a folder) / "FILE_MIMETYPE"(if is a file), "title":"FOLDER_NAME" / "FILE_NAME", ... }

And here is the code I used to access to Google APIs: (VB.NET)

...

Public Function browseFile(ByVal fId As String) As List (Of FileSourceElement)

  Dim webclient As New WebClient()
  webclient.Headers.Add("Authorization", " Bearer " & _accessToken)
  webclient.Headers.Add("X-JavaScript-User-Agent", "Google APIs Explorer")
  Dim res As String = webclient.DownloadString("https://www.googleapis.com/drive/v2/files?key=" & _apiKey).Trim()

  'res contains the JSON with the informations of the file/folder 
  ...

End Function

Public Sub createContainer(ByVal path As String)
  Dim webclient As New WebClient()
  webclient.Headers.Add("Authorization", " Bearer " & _accessToken)
  webclient.Headers.Add("X-JavaScript-User-Agent", "Google APIs Explorer")
  webclient.Headers.Add("Content-Type", "application/json")
  webclient.UploadString("https://www.googleapis.com/drive/v2/files", _ 
                     "POST", _ 
                     "{""title"":""daEliminare2"", ""parents"":[{""id"":""" & path & """}], ""mimeType"":""application/vnd.google-apps.folder""}")
End Sub

...

In my Google Drive I haven't MANUALLY created a folder and I haven't MANUALLY uploaded a file, i must do all the operation only via code.

As I said to you, I've successfully created a folder and I printed successfully the list of file (via code), but in my GDrive the elements created by UI don't appear. Why?


I've read that in the Service Account, in order to get the Access Token it's required a special JSON string called Json Web Token (JWT) composed by an header, a claim set and a signature (base64 string). The string is:

{base64 formatted JWT header}.{base64 formatted JWT claimSet}.{base64 formatted JWT signature}

My JWT is:

Header

{
  "alg": "RS256",
  "typ": "JWT"
}

Claim set { "iss": "[email protected]", // where '0123456789' is the CLIENT_ID. "scope": "https://www.googleapis.com/auth/drive", "aud": "https://accounts.google.com/o/oauth2/token", "exp": timeStamp + 3600, //where 'timeStamp' is the number of seconds since 1970-01-01T00:00:00. "iat": timeStamp }

To write a signature I've followed the procedure descripted in this page:

https://developers.google.com/accounts/docs/OAuth2ServiceAccount#computingsignature

Once I've written the JWT I send it to the server and I get a JSON string as response in this mode:

{
  "access_token": "ACCESS_TOKEN", // (e.g.: 1/8xbJqaOZXS...)
  "type_token": "Bearer",
  "expire_in": 3600
}

When I obtain the Access Token (AT), I use it to access to the Google APIs; for example: if I would show all list of files and folders, I send a request that has an header like this:

Authorization: Bearer AT X-JavaScript-User-Agent: Google APIs Explorer

url: https://www.googleapis.com/drive/v2/files?key={MY_API_KEY}

But I underline that in the list there are some items (using the VB.NET console application), while there aren't any in my Google Drive.

So I got the Access Token but I don't succeed to access to the Google APIs.

PS: I need access to the Google APIs without use any browser.

Part second:

I've read that in the Service Account, in order to get the Acces Token it's required a special JSON string called Json Web Token (JWT) composed by an header, a claim set and a signature (base64 string). The string is:

{base64 formatted JWT header}.{base64 formatted JWT claimSet}.{base64 formatted JWT signature}

My JWT is:

Header

{
  "alg": "RS256",
  "typ": "JWT"
}

Claim set { "iss": "[email protected]", // where '0123456789' is the CLIENT_ID. "scope": "https://www.googleapis.com/auth/drive", "aud": "https://accounts.google.com/o/oauth2/token", "exp": timeStamp + 3600, //where 'timeStamp' is the number of seconds since 1970-01-01T00:00:00. "iat": timeStamp }

To write a signature I've followed the procedure descripted in this page:

https://developers.google.com/accounts/docs/OAuth2ServiceAccount#computingsignature

Once I've written the JWT I send it to the server and I get a JSON string as respoonse in this mode:

{
  "access_token": "ACCESS_TOKEN", // (e.g.: 1/8xbJqaOZXS...)
  "type_token": "Bearer",
  "expire_in": 3600
}

When I obtain the Access Token (AT), I use it to access to the Google APIs; for example: if I would show all list of files and folders, I send a request that has an header like this:

Authorization: Bearer AT X-JavaScript-User-Agent: Google APIs Explorer

url: https://www.googleapis.com/drive/v2/files?key={MY_API_KEY}

But I underline that in the list there are some items (using the VB.NET console application), while there aren't any in my Google Drive.

So I got the Access Token but I don't succeed to access to the Google APIs.

PS: I need access to the Google APIs without use any browser.

Many thanks and best regards

MP

like image 880
Michele Avatar asked Feb 19 '23 08:02

Michele


1 Answers

Since you are using a Service Account, all the folders and files will be created in this Service Account's Drive which cannot be accessed through a web UI and will be limited to the default quota.

To add content in a user's Drive, you will need to go through the regular OAuth 2.0 flow to retrieve credentials from this user. You can find more information about OAuth 2.0 on this pages:

  • Retrieve and use OAuth 2.0 credentials.
  • Quickstart: it has a quickstart sample in C# that you could use.
  • Using OAuth 2.0 to access Google APIs
like image 91
Alain Avatar answered Apr 25 '23 18:04

Alain