我在 Google 云端硬盘中看不到通过代码创建的文件和文件夹 [英] I can't see the files and folders created via code in my Google Drive

查看:42
本文介绍了我在 Google 云端硬盘中看不到通过代码创建的文件和文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名网络开发人员,我想从我的业务软件中访问 Google API,我已经创建了一个云端硬盘帐户我想通过代码(VB.NET)创建一个文件夹和文件.我正在关注文档,我创建了一个 JWT(Json Web 令牌),我使用了一个服务帐户我已经获得了一个访问令牌.

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.

现在我想访问 Google API.使用控制台应用程序,我创建了一些文件夹,并且可以通过代码获取已创建文件夹的列表.但我在我的 Google Drive 帐户中看不到这些文件夹.这怎么可能?

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?

这是我发送到服务器以显示文件夹和文件列表的请求:https://www.googleapis.com/files/{FILE_ID}?key={API_KEY}

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}

我获得一个 JSON 字符串作为响应,其中包含与 FILE_ID 元素相关的信息.例如:

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

{种类":驱动器#文件","id":"FILE_ID","mimeType":"application/vnd.google-apps.folder"(如果是文件夹)/"FILE_MIMETYPE"(如果是文件),"title":"FOLDER_NAME"/"FILE_NAME",...}

{ "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", ... }

这是我用来访问 Google API 的代码:(VB.NET)

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

...

在我的 Google 云端硬盘中,我没有手动创建文件夹,也没有手动上传文件,我只能通过代码完成所有操作.

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.

正如我对您所说,我已经成功创建了一个文件夹,并且我成功打印了文件列表(通过代码),但是在我的 GDrive 中,由 UI 创建的元素不要出现.为什么?

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?

我在服务帐户中读到过,为了获取访问令牌,需要一个名为 Json Web Token (JWT) 的特殊 JSON 字符串,该字符串由标头组成,声明集和签名(base64 字符串).字符串是:

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 格式的 JWT 标头}.{base64 格式的 JWT 声明集}.{base64 格式的 JWT 签名}

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

我的智威汤逊是:

标题

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

索赔集{"iss": "0123456789@developer.gserviceaccount.com",//其中 '0123456789' 是 CLIENT_ID.范围":https://www.googleapis.com/auth/drive","aud": "https://accounts.google.com/o/oauth2/token","exp": timeStamp + 3600,//其中 'timeStamp' 是自 1970-01-01T00:00:00 以来的秒数.iat":时间戳}

Claim set { "iss": "0123456789@developer.gserviceaccount.com", // 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

写完 JWT 后,我将它发送到服务器,并在此模式下得到一个 JSON 字符串作为响应:

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
}

当我获得访问令牌 (AT) 时,我使用它来访问 Google API;例如:如果我要显示所有文件和文件夹列表,我发送的请求具有这样的标头:

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:

授权:承载ATX-JavaScript-User-Agent:Google APIs Explorer

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

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

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

但我强调,列表中有一些项目(使用 VB.NET 控制台应用程序),而我的 Google Drive 中没有.

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.

所以我获得了访问令牌,但我没有成功访问 Google API.

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

PS:我需要在不使用任何浏览器的情况下访问 Google API.

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

第二部分:

我在服务帐户中读到过,为了获取访问令牌,需要一个名为 Json Web 令牌 (JWT) 的特殊 JSON 字符串,该字符串由标头组成,声明集和签名(base64 字符串).字符串是:

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 格式的 JWT 标头}.{base64 格式的 JWT 声明集}.{base64 格式的 JWT 签名}

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

我的智威汤逊是:

标题

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

索赔集{"iss": "0123456789@developer.gserviceaccount.com",//其中 '0123456789' 是 CLIENT_ID.范围":https://www.googleapis.com/auth/drive","aud": "https://accounts.google.com/o/oauth2/token","exp": timeStamp + 3600,//其中 'timeStamp' 是自 1970-01-01T00:00:00 以来的秒数.iat":时间戳}

Claim set { "iss": "0123456789@developer.gserviceaccount.com", // 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

编写 JWT 后,我将其发送到服务器,并在此模式下得到一个 JSON 字符串作为响应:

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
}

当我获得访问令牌 (AT) 时,我使用它来访问 Google API;例如:如果我要显示所有文件和文件夹列表,我发送的请求具有这样的标头:

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:

授权:承载ATX-JavaScript-User-Agent:Google APIs Explorer

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

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

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

但我强调,列表中有一些项目(使用 VB.NET 控制台应用程序),而我的 Google Drive 中没有.

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.

所以我获得了访问令牌,但我没有成功访问 Google API.

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

PS:我需要在不使用任何浏览器的情况下访问 Google API.

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

非常感谢和最好的问候

MP

推荐答案

由于您使用的是服务帐户,所有文件夹和文件都将在此服务帐户的驱动器中创建,无法通过 Web UI 访问,并且会受到限制到默认配额.

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.

要在用户的云端硬盘中添加内容,您需要通过常规 OAuth 2.0 流程来检索该用户的凭据.您可以在此页面上找到有关 OAuth 2.0 的更多信息:

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

这篇关于我在 Google 云端硬盘中看不到通过代码创建的文件和文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆