I don’t see files and folders created using code in my Google Drive

I am a web developer, and I would like to access the Google APIs from my business software, I created an account in Drive and I would create folders and files using code (VB.NET). I follow the documentation, I created a JWT (Json Web Token), I used a service account and I got an access token.

Now I would like to access the Google API. Using the console application, I created several folders, and I can get a list of the created folders using code. But I can’t see these folders in my Google Drive account. How is this possible?

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

I get a JSON string as a response, with information related to FILE_ID elements. eg:.

{"View": "drive # file", "Identifier": "file_id", "mimeType": "application / vnd.google-apps.folder" (if it is a folder) / "FILE_MIMETYPE" (if it is a file), " title ":" FOLDER_NAME "/" FILE_NAME ", ...}

And here is the code that I used to access the Google API: (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 did not create the MOST folder, and I did not upload the MANUALLY file, I have to complete the entire operation with the code only.

As I told you, I successfully created the folder, and I successfully printed the list of files (via code), but the elements created by the user interface do not appear in my GDrive. Why?


I read that in the service account, to get an access token, you need a special JSON string called Json Web Token (JWT), consisting of a header, a set of requirements and a signature (base64 string). Line:

{JWT header in base64 format}. {base64 in JWT ClaimSet format}. {base64 formatted JWT signature}

My JWT:

Headline

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

The set of requirements is {"iss": " 0123456789@developer.gserviceaccount.com ", // where "0123456789" is the identifier 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 performed the procedure described on this page:

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

As soon as I wrote JWT, I send it to the server, and in this mode I get the JSON string as the response:

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

When I receive an access token (AT), I use it to access the Google API; for example: if I displayed the entire list of files and folders, I send a request with the header:

Authorization: AT X-JavaScript-User-Agent Media: Google API Explorer

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

But I emphasize that there are some elements in the list (using the VB.NET console application), although they are not on my Google Drive.

So, I got the access token, but I was not able to access the Google API.

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

Part two:

I read that in the service account, to get Tokes Token, you need a special JSON string called Json Web Token (JWT), consisting of a header, a set of requirements and a signature (base64 string). Line:

{JWT header in base64 format}. {base64 in JWT ClaimSet format}. {base64 formatted JWT signature}

My JWT:

Headline

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

The set of requirements is {"iss": " 0123456789@developer.gserviceaccount.com ", // where "0123456789" is the identifier 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 performed the procedure described on this page:

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

Once I wrote JWT, I send it to the server and I get the JSON string as repoonse in this mode:

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

When I receive an access token (AT), I use it to access the Google API; for example: if I displayed the entire list of files and folders, I send a request with the header:

Authorization: AT X-JavaScript-User-Agent Media: Google API Explorer

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

But I emphasize that there are some elements in the list (using the VB.NET console application), although they are not on my Google Drive.

So, I got the access token, but I was not able to access the Google API.

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

Many thanks and best wishes

MP

+2
source share
4 answers

Because you are using a service account, all folders and files will be created on this Drive service account, which cannot be accessed through the web interface and will be limited by default quota.

To add content to a user drive, you need to go through a regular OAuth 2.0 stream to get credentials from that user. More information about OAuth 2.0 can be found on the following pages:

+4
source

If you want to see files under a user account without using OAuth 2.0, you can grant permission to edit the service account to a folder under the user account.

Then, when downloading the file using the service account, make sure that the folder ID is used for the parent link ID parameter

Folder ID is defined in the address bar

Hope this helps.

+1
source

I did not know about this decision. I did it differently by adding file permission after adding the file (NodeJS code. InsertFile is called after the classic user_pload file):

 insertPermission = function(fileId, value, type, role,authClient,cb) { var body = { 'value': value, 'type': type, 'role': role }; var request = drive.permissions.insert({ 'fileId': fileId, 'resource': body, 'auth': authClient }, function (err,response) { cb(err,response); }); } exports.insertFile = function(file,customer,callback) { exports.authorize(function (err,authClient) { drive.files.insert({ resource: { title: file.name, mimeType: file.mimeType, parents: [{id:customer.googleDriveFolder}] }, media: { mimeType: file.mimeType, body: file.buffer }, auth: authClient }, function(err, response) { debug('error:', err, 'inserted:', response.id); if (!err) { insertPermission(response.id,customer.email,"user","writer",authClient,function (err,resp){ callback(null, resp); }); } }); }); }; 

Note that I am using JWT with a private authentication type with a service account:

 exports.authorize = function(callback) { if (_tokens && ((_tokens.expiry_date - (new Date()).getTime())>100 )) callback(null, _authClient); else { var scope = ['https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/drive.appdata', 'https://www.googleapis.com/auth/drive.apps.readonly', 'https://www.googleapis.com/auth/drive.file','https://www.googleapis.com/auth/drive.metadata', 'https://www.googleapis.com/auth/drive.metadata.readonly', 'https://www.googleapis.com/auth/drive.readonly', 'https://www.googleapis.com/auth/drive.scripts']; var authClient = new google.auth.JWT( '<myServiceAccountClientId - xxx@developer.gserviceaccount.com >', '<path to key>.pem', ''); //seems we could use an user to impersonate requests, i need to test that.. authClient.authorize(function(err, tokens) { if (err) { callback(err); return; } callback(null,authClient); }); } }; 
0
source

You can view your files uploaded using the API. Use the follwing url to view all your files.

https://drive.google.com/folderview?id=XXX

Where XXX = Folder ID created using the API. Debug code to get the folder ID,

But to work on the link you need to provide permission after creating any folder or file, as shown below:

  Permission newPermission = new Permission(); newPermission.Value = " youremail@gmail.com "; newPermission.Type = "anyone"; newPermission.Role = "reader"; _service.Permissions.Insert(newPermission, NewDirectory.Id).Execute(); 

Alternative UI connection: https://drive.google.com/drive/folders/XXX

0
source

Source: https://habr.com/ru/post/1436495/


All Articles