使用 Google Drive REST API v3 和 Angular 2 上传并命名文件
Upload and Name a File Using Google Drive REST API v3 and Angular 2
我正在使用 Angular 中的 Drive REST Api v3 创建一个 Google Drive 服务 2. 大多数功能都已到位:查看文件、下载、创建等.. 但我找不到如何命名文件(创建文件或更新文件时)。
我正在使用以下文档页面:create and update。他们说文件名应该是请求正文的一部分。来自我的 Google Drive 服务的相关代码如下。
createFile(name :string, content :string) :Promise<Object> {
let headers = new Headers({
'Content-Type': 'text/markdown',
'Authorization': 'Bearer ' + this.token,
'name': name //TODO name not working!
});
let options = new RequestOptions({ headers: headers }); // Create a request option
return this.http
.post('https://www.googleapis.com/upload/drive/v3/files' + '?uploadType=multipart', content, options)
.toPromise();
}
updateFile(id :string, content :string, name :string) :Promise<Object> {
let headers = new Headers({
'Content-Type': 'text/markdown',
'Authorization': 'Bearer ' + this.token,
'id': id,
'name': name //TODO name not working!
}); //generate headers
let options = new RequestOptions({ headers: headers }); // Create a request option
return this.http
.patch('https://www.googleapis.com/upload/drive/v3/files/' + id + '?uploadType=multipart', content, options)
.toPromise();
}
总而言之,文件正在创建和更新(包括内容),但命名和重命名文件根本不起作用。
感谢您的帮助。
您正在尝试使用 http header 设置名称。这是错误的。我开始无法理解您是如何认为这样做的,因此您需要返回并重新阅读 Drive API 文档。
简而言之,name: "name"
应该是 JSON object 在请求的 body 中传递,而不是在 http header 中传递。
尝试将名称放在请求 body 中而不是 header 中,如 Files: create:
中所述
Request body
- In the request body, supply a Files resource with the following properties as the metadata. For more information, see the document on media upload.
要对其进行测试,请尝试使用 API Explorer 来帮助您以交互方式探索各种 Google API。
样品请求:
POST https://www.googleapis.com/drive/v3/files?key={YOUR_API_KEY}
{
"name": "My File"
}
回复:
200
{
"kind": "drive#file",
"id": "fileID",
"name": "My File"
}
还有一个相关的SO post解释了如何通过API驱动器将文件插入Google。
希望对您有所帮助。
我也遇到过这个问题。我认为有 3 种解决方案:
对文件元数据和实际文件使用 multipart upload
https://developers.google.com/drive/v3/web/multipart-upload 和不同的 headers。我自己被困在那里,没有找到如何在 Angular 2+
中为单独的请求 headers 添加边界
分两次请求上传文件。首先使用元数据创建空文件(响应将提供文件的 ID),然后实际 "update" 文件。
使用resumable upload
。首先向 "setup metadata" 请求(甚至不会创建空文件)并获取 "special link" 向何处发送请求以上传实际文件。而且这种方法还有一些其他的特点,比如分块上传。https://developers.google.com/drive/v3/web/resumable-upload
这是 link 另一个问题,在 Angular 2 和 DRIVE REST API V3
中实现可恢复上传
Angular 2+ HTTP POST and GDrive API. Resumable file upload with name
我希望它可能有用。
我正在使用 Angular 中的 Drive REST Api v3 创建一个 Google Drive 服务 2. 大多数功能都已到位:查看文件、下载、创建等.. 但我找不到如何命名文件(创建文件或更新文件时)。
我正在使用以下文档页面:create and update。他们说文件名应该是请求正文的一部分。来自我的 Google Drive 服务的相关代码如下。
createFile(name :string, content :string) :Promise<Object> {
let headers = new Headers({
'Content-Type': 'text/markdown',
'Authorization': 'Bearer ' + this.token,
'name': name //TODO name not working!
});
let options = new RequestOptions({ headers: headers }); // Create a request option
return this.http
.post('https://www.googleapis.com/upload/drive/v3/files' + '?uploadType=multipart', content, options)
.toPromise();
}
updateFile(id :string, content :string, name :string) :Promise<Object> {
let headers = new Headers({
'Content-Type': 'text/markdown',
'Authorization': 'Bearer ' + this.token,
'id': id,
'name': name //TODO name not working!
}); //generate headers
let options = new RequestOptions({ headers: headers }); // Create a request option
return this.http
.patch('https://www.googleapis.com/upload/drive/v3/files/' + id + '?uploadType=multipart', content, options)
.toPromise();
}
总而言之,文件正在创建和更新(包括内容),但命名和重命名文件根本不起作用。
感谢您的帮助。
您正在尝试使用 http header 设置名称。这是错误的。我开始无法理解您是如何认为这样做的,因此您需要返回并重新阅读 Drive API 文档。
简而言之,name: "name"
应该是 JSON object 在请求的 body 中传递,而不是在 http header 中传递。
尝试将名称放在请求 body 中而不是 header 中,如 Files: create:
中所述Request body
- In the request body, supply a Files resource with the following properties as the metadata. For more information, see the document on media upload.
要对其进行测试,请尝试使用 API Explorer 来帮助您以交互方式探索各种 Google API。
样品请求:
POST https://www.googleapis.com/drive/v3/files?key={YOUR_API_KEY}
{
"name": "My File"
}
回复:
200
{
"kind": "drive#file",
"id": "fileID",
"name": "My File"
}
还有一个相关的SO post解释了如何通过API驱动器将文件插入Google。
希望对您有所帮助。
我也遇到过这个问题。我认为有 3 种解决方案:
对文件元数据和实际文件使用
multipart upload
https://developers.google.com/drive/v3/web/multipart-upload 和不同的 headers。我自己被困在那里,没有找到如何在 Angular 2+ 中为单独的请求 headers 添加边界
分两次请求上传文件。首先使用元数据创建空文件(响应将提供文件的 ID),然后实际 "update" 文件。
使用
resumable upload
。首先向 "setup metadata" 请求(甚至不会创建空文件)并获取 "special link" 向何处发送请求以上传实际文件。而且这种方法还有一些其他的特点,比如分块上传。https://developers.google.com/drive/v3/web/resumable-upload
这是 link 另一个问题,在 Angular 2 和 DRIVE REST API V3
中实现可恢复上传Angular 2+ HTTP POST and GDrive API. Resumable file upload with name
我希望它可能有用。