通过REST v3上传的文件显示为“未命名” [英] File upload via REST v3 appears as "Untitled"

查看:108
本文介绍了通过REST v3上传的文件显示为“未命名”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过REST(无SDK)在Drive中创建一个文件,遵循官方API:
https://developers.google.com/drive/v3/reference/files/create



所以我请遵循以下两个步骤:
$ b $ ol
创建文件元数据(名称等)

  • 上传文件内容
  • li>

    第二步就像一个魅力一样,然而我在步骤1中遇到了一些问题.Mime类型设置的很好,但至于名字我总是获得无标题文件,无论实际名称或类型如何。



    首先我试过这个:

      POST https://www.googleapis.com/upload/drive/v3/files 
    授权:承载人< OAuth 2.0在此存取令牌>
    Content-Type:image / png

    {
    name:test123.png
    }

    部分工作 - 创建文件,正确设置内容类型(例如,在第2步中写入数据后,我可以在Drive中预览它),但是名字是Untitled。

    然后我再次查看文档,发现以下内容:
    元数据URI,仅用于元数据请求: POST https://www.googleapis.com/drive/v3/files - - >返回404(未找到)。

    在此之后,我尝试了API Explorer中的API: https://developers.google.com/drive/v3/reference/files/create#try-it
    ,注意到这个调用实际上是针对另一个URI(文件被成功创建并按预期命名):

      POST https://content.googl eapis.com/drive/v3/files 
    Content-Type:application / json

    {
    name:test123.png
    }

    当我在我的代码中尝试这个时,我得到了400个错误的请求。



    我觉得我错过了一些东西......任何人都可以帮忙吗?

    解决方案

    那么,如果你有一个来自API浏览器的成功的元数据POST,然后你的代码完全发布相同的请求,那当然必须有效。



    您得到一个标题为无标题的文件的原因是您正在POST内容端点。你在说嘿Google,用这个内容创建一个新文件(POST),但是没有元数据。

    从你提供的代码片断中,我的猜测是您使用的是错误的Content-Type。对于元数据帖子( https://content.googleapis.com/drive/v3/files content-type 总是 application / json 。生成的Drive文件的mime类型应该由json中的mime-type属性设置。



    我建议退后一步并确保您明白,在Drive中,文件(由其元数据表示)与文件内容是分开的。阅读关于分段上传的内容,如果您希望在单次调用中提供,或者更好的还是因为它是图片,请阅读可恢复的上传内容。


    I am trying to create a file in Drive via REST (no SDKs), following the official APIs: https://developers.google.com/drive/v3/reference/files/create

    So I follow two steps:

    1. Create file metadata (name, etc)
    2. Upload file content

    Step 2 works like a charm, however I face some issues with step 1. Mime type is set fine, but as for the name I always get "Untitled" files, no matter of the actual name or type.

    First I tried this:

    POST https://www.googleapis.com/upload/drive/v3/files
    Authorization: Bearer <OAuth 2.0 access token here>
    Content-Type: image/png
    
    {
      "name": "test123.png"
    }
    

    Works partially - the file is created, content type is set correctly (e.g. after I write the data in step 2, I can actually preview it in Drive), but the name is "Untitled".

    Then I looked again in the docu, and found the following: " Metadata URI, for metadata-only requests: POST https://www.googleapis.com/drive/v3/files " --> this returns 404 (not found).

    After this I have tried the API in the API Explorer: https://developers.google.com/drive/v3/reference/files/create#try-it and noticed, that the call is actually against another URI (the file is successfully created and named as expected):

    POST https://content.googleapis.com/drive/v3/files
    Content-Type: application/json
    
    {
      "name": "test123.png"
    }
    

    When I try this in my code, I get 400 Bad Request. Of course I am adding also the bearer token authentication.

    I feel I am missing something... can anyone help?

    解决方案

    Well if you have a successful meta-data POST from the API explorer, and then your code issues exactly the same request, then of course it must work.

    The reason you are getting a file with a title of "Untitled" is that you are POSTing to content endpoint. You are saying "hey Google, create a new file (POST) with this content, but no metadata".

    From the snippets you've provided, my guess would be that you are using the wrong Content-Type. For a metadata post (https://content.googleapis.com/drive/v3/files) the content-type is always application/json. The mime-type of the resulting Drive file should be set by the mime-type property in the json.

    I suggest take a step back and make sure you understand that, in Drive, the file (as represented by its metadata) is separate to the file contents. Read about "multipart uploads" if you want to provide both in a single call, or better still because it's an image, read about resumable uploads.

    这篇关于通过REST v3上传的文件显示为“未命名”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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