{"id":207129,"date":"2025-07-08T09:33:44","date_gmt":"2025-07-08T01:33:44","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207129\/"},"modified":"2025-07-08T09:33:44","modified_gmt":"2025-07-08T01:33:44","slug":"%e4%bb%8e-golang-%e4%bb%a3%e7%a0%81%e5%b0%86%e6%96%87%e4%bb%b6%e5%8f%91%e9%80%81%e5%88%b0-google-drive-api-%e4%bc%9a%e4%ba%a7%e7%94%9f%e9%94%99%e8%af%af%ef%bc%9a%e7%b1%bb%e5%9e%8b%e4%b8%8d%e6%94%af","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207129\/","title":{"rendered":"\u4ece golang \u4ee3\u7801\u5c06\u6587\u4ef6\u53d1\u9001\u5230 Google Drive API \u4f1a\u4ea7\u751f\u9519\u8bef\uff1a\u7c7b\u578b\u4e0d\u652f\u6301\u7684\u5185\u5bb9\uff1aimage\/jpeg"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u4ece golang \u4ee3\u7801\u5c06\u6587\u4ef6\u53d1\u9001\u5230 Google Drive API \u4f1a\u4ea7\u751f\u9519\u8bef\uff1a\u7c7b\u578b\u4e0d\u652f\u6301\u7684\u5185\u5bb9\uff1aimage\/jpeg<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-20 11:06:35<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u6765\u5230\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5e0c\u671b\u5728\u8fd9\u91cc\u5b66\u4e60Golang\u76f8\u5173\u7f16\u7a0b\u77e5\u8bc6\u3002\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u5e26\u5927\u5bb6\u804a\u804a\u300a\u4ece golang \u4ee3\u7801\u5c06\u6587\u4ef6\u53d1\u9001\u5230 Google Drive API \u4f1a\u4ea7\u751f\u9519\u8bef\uff1a\u7c7b\u578b\u4e0d\u652f\u6301\u7684\u5185\u5bb9\uff1aimage\/jpeg\u300b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6839\u636e google drive api \u6587\u6863\uff0c\u4e0a\u4f20\u6587\u4ef6\u7684\u6b63\u786e\u65b9\u6cd5\u662f\uff1a<\/p>\n<p><code>curl -v -h '\u6388\u6743\uff1a\u4e0d\u8bb0\u540d mytoken' -f 'metadata={\"name\": \"test3.jpeg\"};type=application\/json' -f file=@jpeg_image.jpeg 'https:\/\/ www.googleapis.com\/upload\/drive\/v3\/files?uploadtype=multipart'<\/code><\/p>\n<p>\u73b0\u5728\uff0c\u6211\u9700\u8981\u4ece golang \u4ee3\u7801\u6267\u884c\u76f8\u540c\u7684\u8bf7\u6c42\uff0c\u4f46\u6211\u5f88\u96be\u5c06\u5176\u8f6c\u6362\u4e3a golang\uff0c\u8fd9\u662f\u6211\u7ecf\u8fc7\u591a\u6b21\u5c1d\u8bd5\u540e\u4f7f\u7528\u7684\u4ee3\u7801\uff1a<\/p>\n<pre>\/\/ filebytes are of type []byte\nbuffer := &amp;bytes.buffer{}\n    multipartwriter := multipart.newwriter(buffer)\n    multipartwriter.writefield(\"metadata\", \"{\\\"name\\\": \\\"test3.jpef\\\"};type=application\/json\")\n    partheader := textproto.mimeheader{}\n    partheader.add(\"content-type\", \"image\/jpeg\")\n    filepartwriter, _ := multipartwriter.createpart(partheader)\n    io.copy(filepartwriter, bytes.newreader(filebytes))\n    multipartwriter.close()\n\n    googledriverequest, _ := http.newrequest(http.methodpost, \"https:\/\/www.googleapis.com\/upload\/drive\/v3\/files?uploadtype=multipart\", buffer)\n    googledriverequest.header.add(\"authorization\", \"bearer \"+accesstoken)\n    googledriverequest.header.add(\"content-type\", multipartwriter.formdatacontenttype())\n\n    googledriveapiresponse, googledriveapierror := lib.httpclient.do(googledriverequest)<\/pre>\n<p>\u4f7f\u7528curl\u8bf7\u6c42\u6210\u529f\uff1a<\/p>\n<pre>{\n \"kind\": \"drive#file\",\n \"id\": \"1dwsbqgp3-qts5p0iqqrtajcccsafxzgd\",\n \"name\": \"test3.jpeg\",\n \"mimetype\": \"image\/jpeg\"\n}<\/pre>\n<p>\u4f7f\u7528 golang\uff0c\u6211\u5f97\u5230 400\uff1a<\/p>\n<pre>{\\n \\\"error\\\": {\\n  \\\"errors\\\": [\\n   {\\n    \\\"domain\\\": \\\"global\\\",\\n    \\\"reason\\\": \\\"badContent\\\",\\n    \\\"message\\\": \\\"Unsupported content with type: image\/jpeg \\\"\\n   }\\n  ],\\n  \\\"code\\\": 400,\\n  \\\"message\\\": \\\"Unsupported content with type: image\/jpeg\\\"\\n }\\n}\\n<\/pre>\n<p>\u6211\u8fd8\u5c1d\u8bd5\u4e86 <code>partheader.add(\"content-type\", \"image\/jpeg\")<\/code> \u7684\u4e0d\u540c\u503c\uff0c\u6211\u4e5f\u5c1d\u8bd5\u4e86 <code>application\/x-www-form-urlencoded<\/code> \uff0c\u6211\u8fd8\u6ce8\u91ca\u6389\u4e86\u8fd9\u4e00\u884c\uff0c\u5e76\u8ba9 golang \u8fdb\u884c\u68c0\u6d4b\uff0c\u4f46\u4ecd\u7136\u51fa\u73b0\u76f8\u540c\u7684\u9519\u8bef\u3002 <\/p>\n<p>\u6709\u4ec0\u4e48\u5efa\u8bae\u6216\u60f3\u6cd5\u5417\uff1f <\/p>\n<p>\u95ee\u5019\uff0c<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u95ee\u9898\u51fa\u5728\u60a8\u53d1\u9001\u7684 json \u5143\u6570\u636e\u4e2d\u3002\u8fd9\u662f\u4e00\u4e2a\u53ef\u4ee5\u5de5\u4f5c\u7684\u793a\u4f8b\u4ee3\u7801\uff08\u4f7f\u7528\u9002\u5f53\u7684\u9519\u8bef\u68c0\u67e5\uff09\uff0c<\/p>\n<pre>import (\n    \"bytes\"\n    \"fmt\"\n    \"io\"\n    \"io\/ioutil\"\n    \"log\"\n    \"mime\/multipart\"\n    \"net\/http\"\n    \"net\/textproto\"\n)\n\nfunc main() {\n    accessToken := \"YOUR-TOKEN\"\n\n    client := http.Client{}\n    mediaData, _ := ioutil.ReadFile(\"test.png\")\n\n    body := &amp;bytes.Buffer{}\n    writer := multipart.NewWriter(body)\n\n    \/\/ JSON Metadata (part-1)\n    jsonMetadata := textproto.MIMEHeader{}\n    metadata := `{\"name\": \"test.png\"}`\n    jsonMetadata.Set(\"Content-Type\", \"application\/json\")\n    part, _ := writer.CreatePart(jsonMetadata)\n    part.Write([]byte(metadata))\n\n    \/\/ Image bytes (part-2)\n    imageData := textproto.MIMEHeader{}\n    partAttach, _ := writer.CreatePart(imageData)\n    io.Copy(partAttach, bytes.NewReader(mediaData))\n    writer.Close()\n\n    \/\/ Request Content Type with boundary\n    contentType := fmt.Sprintf(\"multipart\/related; boundary=%s\", writer.Boundary())\n\n    \/\/ HTTP Request with auth and content type headers\n    req, _ := http.NewRequest(http.MethodPost, \"https:\/\/www.googleapis.com\/upload\/drive\/v3\/files?uploadType=multipart\", bytes.NewReader(body.Bytes()))\n    req.Header.Add(\"Authorization\", \"Bearer \"+accessToken)\n    req.Header.Add(\"Content-Type\", contentType)\n\n    \/\/ Send request\n    resp, err := client.Do(req)\n    if err != nil {\n        log.Fatalf(\"failed to send request: %v\", err)\n    }\n    defer resp.Body.Close()\n    content, _ := ioutil.ReadAll(resp.Body)\n\n    log.Printf(\"http status: %d\", resp.StatusCode)\n    log.Printf(\"response: %s\", string(content))\n}<\/pre>\n<p>\u53c2\u8003\u53f7\uff1a<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u4ece golang \u4ee3\u7801\u5c06\u6587\u4ef6\u53d1\u9001\u5230 Google Drive API \u4f1a\u4ea7\u751f\u9519\u8bef\uff1a\u7c7b\u578b\u4e0d\u652f\u6301\u7684\u5185\u5bb9\uff1aimage\/jpeg\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207129","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207129","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207129"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207129\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}