Python Forum
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 q 
output :
Output:
('OK', ['[email protected] authenticated (Success)'])
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")
Output:
/root/Downloads/recovery.pdf /root/Downloads/1-s2.0-S1742287618300409-main.pdf /root/Downloads/85926-1.pdf /root/Downloads/85926(1).pdf /root/Downloads/85926.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...

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."
i found something here :
https://developers.google.com/identity/protocols/OAuth2WebServer#offline

Quote:Refreshing an access token (offline access)

Access tokens periodically expire. You can refresh an access token without prompting the user for permission (including when the user is not present) if you requested offline access to the scopes associated with the token.

If you use a Google API Client Library, the client object refreshes the access token as needed as long as you configure that object for offline access.
If you are not using a client library, you need to set the access_type HTTP query parameter to offline when redirecting the user to Google's OAuth 2.0 server. In that case, Google's authorization server returns a refresh token when you exchange an authorization code for an access token. Then, if the access token expires (or at any other time), you can use a refresh token to obtain a new access token.

Requesting offline access is a requirement for any application that needs to access a Google API when the user is not present. For example, an app that performs backup services or executes actions at predetermined times needs to be able to refresh its access token when the user is not present. The default style of access is called online.

Server-side web applications, installed applications, and devices all obtain refresh tokens during the authorization process. Refresh tokens are not typically used in client-side (JavaScript) web applications.

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 ?