how to upload a file to my gmail driver after login ?? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: how to upload a file to my gmail driver after login ?? (/thread-13861.html) |
how to upload a file to my gmail driver after login ?? - evilcode1 - Nov-04-2018 hey folk ,,, i already write a code to login into my gmail ... my code : import imaplib enc1 = ("password") #=================================================>>>> change ur Password from here ... enc2 = ("[email protected]") #=====================================>>>> change ur E-mail from here ..... conn = imaplib.IMAP4_SSL("imap.gmail.com", 993) q= conn.login(enc2, enc1) #Login to Google Drive and create drive object print qoutput : ok now how i can upload files in my ( /root/Downloads ) to my gmail drive ??
RE: how to upload a file to my gmail driver after login ?? - evilcode1 - Nov-04-2018 i found a solution ... but i need to upload multi files !! this is my code : import json import requests import glob def upload_1(): headers = {"Authorization": "Bearer access token"} #put ur access token after the word 'Bearer ' qassam = glob.glob("/root/Downloads/*.pdf") for i in qassam: qassam = "\n".join(qassam) print i para = { "name": "/root/Downloads/Procdump.zip", #file name to be uploaded "parents": ["1bEBQugJ4GVnEECElKztEgq9tHbFdYetu"] # make a folder on drive in which you want to upload files; then open that folder; the last thing in present url will be folder id } files = { 'data': ('metadata', json.dumps(para), 'application/json; charset=UTF-8'), 'file': ('application/zip',open("/root/Downloads/Procdump.zip", "rb")) # replace 'application/zip' by 'image/png' for png images; similarly 'image/jpeg' (also replace your file name) } r = requests.post( "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart", headers=headers, files=files ) print(r.text) upload_1()i need to upload all pdf files in my /root/Download this is the output for glob.glob("/root/Downloads/*.pdf")
RE: how to upload a file to my gmail driver after login ?? - evilcode1 - Nov-05-2018 i solved the multi files issue by adding (i) import json import requests import glob def upload_1(): headers = {"Authorization": "Bearer access token"} #put ur access token after the word 'Bearer ' qassam = glob.glob("/root/Downloads/*.pdf") for i in qassam: qassam = "\n".join(qassam) print i para = { "name": (i), #file name to be uploaded "parents": ["1bEBQugJ4GVnEECElKztEgq9tHbFdYetu"] # make a folder on drive in which you want to upload files; then open that folder; the last thing in present url will be folder id } files = { 'data': ('metadata', json.dumps(para), 'application/json; charset=UTF-8'), 'file': ('application/zip',open(i, "rb")) # replace 'application/zip' by 'image/png' for png images; similarly 'image/jpeg' (also replace your file name) } r = requests.post( "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart", headers=headers, files=files ) print(r.text) upload_1()but i have a new issue !! the access token is expired every 1 hour !! so i must go the : https://developers.google.com/oauthplayground/ and get the new one and replace it manually !! i dont need that i need to do this auto !! is there anyway to get a new token by python !! RE: how to upload a file to my gmail driver after login ?? - evilcode1 - Nov-05-2018 any help ? RE: how to upload a file to my gmail driver after login ?? - micseydel - Nov-05-2018 Your question seems to be more about Google APIs or even Oauth but not so much Python. That said... My understanding of oauth tokens is that they're supposed to be short-lived. I don't know a lot about Google APIs, but you need to find a way to generate a token which doesn't expire so frequently. There should be a developer console or something where you can do that, which isn't just a "playground." RE: how to upload a file to my gmail driver after login ?? - evilcode1 - Nov-06-2018 (Nov-05-2018, 08:30 PM)micseydel Wrote: Your question seems to be more about Google APIs or even Oauth but not so much Python. That said...i found something here : https://developers.google.com/identity/protocols/OAuth2WebServer#offline Quote:Refreshing an access token (offline access) authorization_url, state = flow.authorization_url( # Enable offline access so that you can refresh an access token without # re-prompting the user for permission. Recommended for web server apps. access_type='offline', # Enable incremental authorization. Recommended as a best practice. include_granted_scopes='true')but i dont know how to use it can anyone explain ? |