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

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

问题描述

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



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

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



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

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



以下是我用于访问Google API的代码:(VB.NET)



$ p
$ b $ pre $ 公共函数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包含带有文件/文件夹信息的JSON
...

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云端硬盘中,我没有手动创建文件夹,而且我也没有手动上传文件,我必须通过代码完成所有操作。



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




我已经读过服务帐户中的要获取Access Token,它需要一个特殊的JSON由标题,
a声明集和一个签名(base64字符串)组成的名为Json Web Token(JWT)的字符串。
字符串是:

ase64格式的JWT头{base64格式的JWT claimSet} {base64格式的JWT签名}

我的JWT是:

标题

  {
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:timeStamp
}



编写一个签名我遵循本页描述的过程:

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



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

  {
access_token:ACCESS_TOKEN,//(例如:1 / 8xbJqaOZXS ...)
type_token:持票人,
expire_in:3600
}



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



授权:持票人AT
X-JavaScript-User-Agent:Google API资源管理器



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



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



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



PS:我需要访问Google API而无需使用任何浏览器。



第二部分:

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

ase64格式的JWT头{base64格式的JWT claimSet} {base64格式的JWT签名}

我的JWT是:

标题

  {
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:timeStamp
}



编写一个签名我遵循本页描述的过程:

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



一旦我编写了JWT,我将它发送到服务器,并在此模式下获得一个JSON字符串作为respoonse:

  {
access_token:ACCESS_TOKEN,//(例如:1 / 8xbJqaOZXS ...)
type_token:持票人,
expire_in:3600
}



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



授权:持票人AT
X-JavaScript-User-Agent:Google API资源管理器



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



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



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



PS:我需要访问Google API而无需使用任何浏览器。



非常感谢和最好的祝愿

MP

解决方案

由于您使用的是服务帐户,所有文件夹和文件都将在此服务帐户的云端硬盘中创建,无法通过网络用户界面进行访问,并且会受到限制编辑为默认配额。



要在用户的云端硬盘中添加内容,您需要通过常规的OAuth 2.0流程来检索此用户的凭据。有关OAuth 2.0的更多信息,请访问:

/ credentialsrel =nofollow>检索并使用OAuth 2.0凭据。
  • 快速入门:它有一个可以在C#中使用的快速入门示例。
  • 使用OAuth 2.0访问Google API

    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": "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

    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": "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

    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

    解决方案

    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:

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

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