我在Google云端硬盘中看不到通过代码创建的文件和文件夹

Mic*_*ele 3 google-drive-api

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

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

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

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

{“ kind”:“ drive#file”,“ id”:“ FILE_ID”,“ mimeType”:“ application / vnd.google-apps.folder”(如果是文件夹)/“ FILE_MIMETYPE”(如果是文件) ,“ title”:“ FOLDER_NAME” /“ FILE_NAME”,...}

这是我用来访问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
Run Code Online (Sandbox Code Playgroud)

...

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

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


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

{base64格式的JWT标头}。{base64格式的JWT ClaimSet}。{base64格式的JWT签名}

我的JWT是:

标头

{
  "alg": "RS256",
  "typ": "JWT"
}
Run Code Online (Sandbox Code Playgroud)

声明集{“” iss“:” 0123456789@developer.gserviceaccount.com“,//其中“ 0123456789”是CLIENT_ID。“ scope”:“ https://www.googleapis.com/auth/drive”,“ aud”:“ https://accounts.google.com/o/oauth2/token”,“ exp”:timeStamp + 3600, //其中“ timeStamp”是自1970-01-01T00:00:00以来的秒数。“ iat”:时间戳记}

要编写签名,我已按照本页中描述的过程进行操作:

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

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

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

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

授权:Bearer AT X-JavaScript-User-Agent:Google APIs Explorer

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

但我强调指出,列表中有一些项目(使用VB.NET控制台应用程序),而我的Google云端硬盘中没有任何项目。

因此,我获得了访问令牌,但未成功访问Google API。

PS:我需要不使用任何浏览器即可访问Google API。

第二部分:

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

{base64格式的JWT标头}。{base64格式的JWT ClaimSet}。{base64格式的JWT签名}

我的JWT是:

标头

{
  "alg": "RS256",
  "typ": "JWT"
}
Run Code Online (Sandbox Code Playgroud)

声明集{“” iss“:” 0123456789@developer.gserviceaccount.com“,//其中“ 0123456789”是CLIENT_ID。“ scope”:“ https://www.googleapis.com/auth/drive”,“ aud”:“ https://accounts.google.com/o/oauth2/token”,“ exp”:timeStamp + 3600, //其中“ timeStamp”是自1970-01-01T00:00:00以来的秒数。“ iat”:时间戳记}

要编写签名,我已按照本页中描述的过程进行操作:

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

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

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

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

授权:Bearer AT X-JavaScript-User-Agent:Google APIs Explorer

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

但我强调指出,列表中有一些项目(使用VB.NET控制台应用程序),而我的Google云端硬盘中没有任何项目。

因此,我获得了访问令牌,但未成功访问Google API。

PS:我需要不使用任何浏览器即可访问Google API。

致以真诚的感谢和诚挚的问候

MP

Ala*_*ain 5

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

要将内容添加到用户的云端硬盘中,您将需要执行常规的OAuth 2.0流程以从该用户获取凭据。您可以在以下页面上找到有关OAuth 2.0的更多信息: