{"id":19651799,"url":"https://github.com/tanaikech/filetransfer","last_synced_at":"2025-04-28T16:31:47.691Z","repository":{"id":50702471,"uuid":"90821358","full_name":"tanaikech/FileTransfer","owner":"tanaikech","description":"How to transfer files for Google Drive without authorization.","archived":false,"fork":false,"pushed_at":"2017-05-10T05:34:17.000Z","size":2,"stargazers_count":9,"open_issues_count":2,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-05T09:51:08.357Z","etag":null,"topics":["file-download","file-upload","google-drive-api","no-authentication"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tanaikech.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-05-10T04:37:11.000Z","updated_at":"2025-02-12T10:19:05.000Z","dependencies_parsed_at":"2022-09-24T16:25:17.714Z","dependency_job_id":null,"html_url":"https://github.com/tanaikech/FileTransfer","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tanaikech%2FFileTransfer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tanaikech%2FFileTransfer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tanaikech%2FFileTransfer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tanaikech%2FFileTransfer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tanaikech","download_url":"https://codeload.github.com/tanaikech/FileTransfer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251345991,"owners_count":21574811,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["file-download","file-upload","google-drive-api","no-authentication"],"created_at":"2024-11-11T15:08:08.714Z","updated_at":"2025-04-28T16:31:47.424Z","avatar_url":"https://github.com/tanaikech.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# File Transfer for Google Drive Without Authorization\r\n\r\n# Overview\r\nIn this article, I would like to introduce how to transfer files for Google Drive under no authorization.\r\n\r\n# Description\r\nWhen we download and upload files for Google Drive, it usually has to use Drive API. In order to use Drive API, access token is required. If you want to make your friends download and upload files for your Google Drive, the authorization process is to take time. So I proposal this.\r\n\r\nAs a sample, I introduce a script for downloading and uploading files using Web Apps. In this sample, it changes a file to a byte slice and send it as text data. Then, it reconstructs it. Of course, base64 encode can be used for this. But the data size for using base64 is much larger than that for using the byte slice.\r\n\r\nAt this method, the project files including GAS script cannot be downloaded. When a script file is uploaded, it is converted to text file which is not a project file of Google. When Google Docs can be downloaded using this method, those are downloaded as PDF file. docx, pptx and xlsx can be uploaded.\r\n\r\nThey say taht the limitation size of an uploading file is 24 MBytes. [Ref.](http://stackoverflow.com/questions/38315816/max-size-for-post-request-sent-to-webapps)\r\n\r\n# Usage\r\n## 1. \u003cu\u003e[Deploy Web Apps](https://developers.google.com/apps-script/guides/web)\u003c/u\u003e\r\n1. Open the Script Editor.\r\n2. On the Script Editor\r\n    - File -\u003e Manage Versions -\u003e Save New Version\r\n    - Publish -\u003e Deploy as Web App\r\n    - At Execute the app as, select **\"your account\"**\r\n    - At Who has access to the app, select **\"Anyone, even anonymous\"**\r\n    - Click \"Deploy\"\r\n    - Copy **\"Current web app URL\"**\r\n    - Click \"OK\"\r\n\r\n## 2. Paste following script on Script Editor.\r\n\r\n**After pasted it, please run ``doPost()`` on Google Script Editor and authorize the script at [Authorization for Google Services](https://developers.google.com/apps-script/guides/services/authorization).** This is an important point.\r\n\r\n~~~javascript\r\nfunction doPost(e) {\r\n  if (e.parameters.method == \"download\") {\r\n    try {\r\n      return (function(id){\r\n        var file = DriveApp.getFileById(id);\r\n        return ContentService\r\n              .createTextOutput(JSON.stringify({\r\n                size: file.getBlob().getBytes(),\r\n                name: file.getName(),\r\n                result: file.getName() + \" (\" + file.getBlob().getContentType() + \")\"\r\n              }))\r\n              .setMimeType(ContentService.MimeType.JSON);\r\n      })(e.parameters.id);\r\n    } catch(err) {\r\n      return ContentService.createTextOutput(JSON.stringify({\r\n                result: err.message\r\n              }))\r\n              .setMimeType(ContentService.MimeType.JSON);\r\n    }\r\n  }\r\n\r\n  if (e.parameters.method == \"upload\") {\r\n    try {\r\n      return ContentService\r\n              .createTextOutput(JSON.stringify({\r\n                result: (function(p){\r\n                  return DriveApp\r\n                    .createFile(\r\n                      Utilities.newBlob(\r\n                        [parseInt(i, 10) for each (i in p.file)],\r\n                        p.mime,\r\n                        p.name\r\n                      )\r\n                    )\r\n                    .getId();\r\n                })(e.parameters)\r\n              }))\r\n              .setMimeType(ContentService.MimeType.JSON);\r\n    } catch(err) {\r\n      return ContentService.createTextOutput(JSON.stringify({\r\n                result: err.message\r\n              }))\r\n              .setMimeType(ContentService.MimeType.JSON);\r\n    }\r\n  }\r\n\r\n  if (e.parameters.method == \"delete\") {\r\n    try {\r\n      DriveApp.getFileById(e.parameters.id).setTrashed(true);\r\n      return ContentService.createTextOutput(JSON.stringify({\r\n                result: e.parameters.id + \" was deleted.\"\r\n              }))\r\n              .setMimeType(ContentService.MimeType.JSON);\r\n    } catch(err) {\r\n      return ContentService.createTextOutput(JSON.stringify({\r\n                result: err.message\r\n              }))\r\n              .setMimeType(ContentService.MimeType.JSON);\r\n    }\r\n  }\r\n\r\n  return ContentService.createTextOutput(\"Did nothing.\");\r\n}\r\n~~~\r\n\r\n## 3. At local pc, use following script.\r\nThere are 3 methods of ``download()``, ``upload()`` and ``delete()``. When you use those, please give a value of file ID to``download()`` and ``delete()``. Then please give file name to ``upload()``.\r\n\r\n~~~python\r\nimport mimetypes\r\nimport numpy as np\r\nimport requests\r\n\r\n# Please paste \"Current web app URL\" here.\r\nurl = \"https://script.google.com/macros/s/#####/exec\"\r\n\r\n\r\ndef download(fileid):\r\n    r = requests.post(\r\n        url,\r\n        data={\"method\": \"download\", \"id\": fileid}\r\n    )\r\n    if len(r.json()[\"name\"]) \u003e 0:\r\n        with open(r.json()[\"name\"], \"bw\") as f:\r\n            f.write(np.array(r.json()[\"size\"], dtype=np.uint8))\r\n    return r.json()[\"result\"]\r\n\r\n\r\ndef upload(filename):\r\n    with open(filename, \"rb\") as f:\r\n        d = f.read()\r\n    r = requests.post(\r\n        url,\r\n        data={\r\n            \"method\": \"upload\",\r\n            \"file\": [(-(i \u0026 0b10000000) | (i \u0026 0b01111111)) for i in d],\r\n            \"name\": filename,\r\n            \"mime\": mimetypes.guess_type(filename)[0]\r\n        }\r\n    )\r\n    return r.json()[\"result\"]\r\n\r\n\r\ndef delete(fileid):\r\n    r = requests.post(\r\n        url,\r\n        data={\"method\": \"delete\", \"id\": fileid}\r\n    )\r\n    return r.json()[\"result\"]\r\n\r\ndef main():\r\n    fileid = \"#####\"\r\n    print(download(fileid))\r\n\r\n    filename = \"#####\"\r\n    print(upload(filename))\r\n\r\n    fileid = \"#####\"\r\n    print(delete(fileid))\r\n\r\nif __name__ == '__main__':\r\n    main()\r\n~~~\r\n\r\nDownload files have no extension. So please add the extension for each mimeType to the files.\r\n\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftanaikech%2Ffiletransfer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftanaikech%2Ffiletransfer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftanaikech%2Ffiletransfer/lists"}