Python Forum
how to upload a file to my gmail driver after login ??
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to upload a file to my gmail driver after login ??
#1
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 ??
Reply
#2
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
Reply
#3
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 !!
Reply
#4
any help ?
Reply
#5
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."
Reply
#6
(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/p...er#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 ?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error (Errno 2), File upload with the Flask framework and a public IP Username_Python1 0 133 Yesterday, 01:46 PM
Last Post: Username_Python1
  Extract PDF Attachment from Gmail jstaffon 0 536 Sep-10-2023, 01:55 PM
Last Post: jstaffon
  '' FTP '' File upload with a specified string and rename midomarc 1 1,105 Apr-17-2023, 03:04 AM
Last Post: bowlofred
  Python code a Max7219/7221 7 segment driver Aggie64 11 3,024 Dec-01-2022, 02:24 AM
Last Post: Larz60+
  Unable to upload file using FTP korenron 2 2,422 Dec-23-2021, 12:44 PM
Last Post: korenron
  Stepper motor/Easy Driver/HELP Harney 1 1,855 Jul-31-2021, 08:05 AM
Last Post: Harney
  Gmail Sent email LIMIT Johnse 5 7,003 Apr-28-2021, 10:19 PM
Last Post: snippsat
  in a login interface when i try login with a user supposed to say test123 but nothing NullAdmin 3 2,218 Feb-20-2021, 04:43 AM
Last Post: bowlofred
Photo Raspberry PI writing to RS485 driver, DIR pin not holding state zazas321 2 2,087 Oct-30-2020, 06:23 AM
Last Post: zazas321
  extract email addresses from gmail vigneshboolog 0 1,732 Feb-11-2020, 09:23 AM
Last Post: vigneshboolog

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020