{"id":3104,"date":"2019-04-30T15:18:25","date_gmt":"2019-04-30T14:18:25","guid":{"rendered":"https:\/\/www.lieben.nu\/liebensraum\/?p=3104"},"modified":"2019-04-30T15:18:25","modified_gmt":"2019-04-30T14:18:25","slug":"uploading-a-file-to-onedrive-for-business-with-python","status":"publish","type":"post","link":"https:\/\/lieben.nu\/liebensraum\/2019\/04\/uploading-a-file-to-onedrive-for-business-with-python\/","title":{"rendered":"Uploading a file to onedrive for business with Python"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">For a Raspberry Pi project that&#8217;ll take a number of pictures of my house for an as of yet unknown period of time I&#8217;m sharing my very first Python script with you.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">All it has to do is upload all files from a given folder to a given Onedrive for Business path, as obviously the Pi can&#8217;t store much data on its tiny SD card. You&#8217;ll need to<a rel=\"noreferrer noopener\" aria-label=\" register an azure ad app (opens in a new tab)\" href=\"https:\/\/docs.microsoft.com\/en-us\/graph\/auth-register-app-v2\" target=\"_blank\"> register an azure ad app<\/a> and give it the<a rel=\"noreferrer noopener\" aria-label=\" appropriate permissions (opens in a new tab)\" href=\"https:\/\/docs.microsoft.com\/en-us\/graph\/api\/driveitem-put-content?view=graph-rest-1.0\" target=\"_blank\"> appropriate permissions<\/a>. You&#8217;ll have to consent to the application once (url format = https:\/\/login.microsoftonline.com\/common\/adminconsent?client_id={client-id}).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Then schedule below Python script on your Pi, it will retrieve an Azure token without the need for external libraries, parse the directory and upload everything in it to the given onedrive for business URL, simple as that! It can also be used for Sharepoint or Teams by adjusting the path.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport requests \nimport json\ndirectory = r\"c:\\temp\\uploads\"\ndata = {'grant_type':\"client_credentials\", \n        'resource':\"https:\/\/graph.microsoft.com\", \n        'client_id':'XXXXX', \n        'client_secret':'XXXXX'} \nURL = \"https:\/\/login.windows.net\/YOURTENANTDOMAINNAME\/oauth2\/token?api-version=1.0\"\nr = requests.post(url = URL, data = data) \nj = json.loads(r.text)\nTOKEN = j&#x5B;\"access_token\"]\nURL = \"https:\/\/graph.microsoft.com\/v1.0\/users\/YOURONEDRIVEUSERNAME\/drive\/root:\/fotos\/HouseHistory\"\nheaders={'Authorization': \"Bearer \" + TOKEN}\nr = requests.get(URL, headers=headers)\nj = json.loads(r.text)\nprint(\"Uploading file(s) to \"+URL)\nfor root, dirs, files in os.walk(directory):\n    for filename in files:\n        filepath = os.path.join(root,filename)\n        print(\"Uploading \"+filename+\"....\")\n        fileHandle = open(filepath, 'rb')\n        r = requests.put(URL+\"\/\"+filename+\":\/content\", data=fileHandle, headers=headers)\n        fileHandle.close()\n        if r.status_code == 200 or r.status_code == 201:\n            #remove folder contents\n            print(\"succeeded, removing original file...\")\n            os.remove(os.path.join(root, filename)) \nprint(\"Script completed\")\nraise SystemExit\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>For a Raspberry Pi project that&#8217;ll take a number of pictures of my house for an as of yet unknown period of time I&#8217;m sharing my very first Python script with you. All it has to do is upload all files from a given folder to a given Onedrive for Business path, as obviously the &hellip; <a href=\"https:\/\/lieben.nu\/liebensraum\/2019\/04\/uploading-a-file-to-onedrive-for-business-with-python\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Uploading a file to onedrive for business with Python<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[4,34,40,44],"tags":[],"class_list":["post-3104","post","type-post","status-publish","format-standard","hentry","category-automation","category-onedrive-for-business","category-python","category-sharepoint-online"],"_links":{"self":[{"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/posts\/3104","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/comments?post=3104"}],"version-history":[{"count":0,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/posts\/3104\/revisions"}],"wp:attachment":[{"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/media?parent=3104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/categories?post=3104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/tags?post=3104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}