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