Posts: 104
Threads: 36
Joined: Oct 2017
hey folk ,,, i already write a code to login into my gmail ... my code :
1 2 3 4 5 6 7 8 9 10 11 12 |
import imaplib
enc1 = ( "password" )
enc2 = ( "email@gmail.com" )
conn = imaplib.IMAP4_SSL( "imap.gmail.com" , 993 )
q = conn.login(enc2, enc1)
print q
|
output :
Output: ('OK', ['email@gmail.com authenticated (Success)'])
ok now how i can upload files in my ( /root/Downloads ) to my gmail drive ??
Posts: 104
Threads: 36
Joined: Oct 2017
i found a solution ... but i need to upload multi files !! this is my code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import json
import requests
import glob
def upload_1():
headers = { "Authorization" : "Bearer access token" }
qassam = glob.glob( "/root/Downloads/*.pdf" )
for i in qassam:
qassam = "\n" .join(qassam)
print i
para = {
"name" : "/root/Downloads/Procdump.zip" ,
"parents" : [ "1bEBQugJ4GVnEECElKztEgq9tHbFdYetu" ]
}
files = {
'data' : ( 'metadata' , json.dumps(para), 'application/json; charset=UTF-8' ),
'file' : ( 'application/zip' , open ( "/root/Downloads/Procdump.zip" , "rb" ))
}
r = requests.post(
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
Posts: 104
Threads: 36
Joined: Oct 2017
i solved the multi files issue by adding (i)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import json
import requests
import glob
def upload_1():
headers = { "Authorization" : "Bearer access token" }
qassam = glob.glob( "/root/Downloads/*.pdf" )
for i in qassam:
qassam = "\n" .join(qassam)
print i
para = {
"name" : (i),
"parents" : [ "1bEBQugJ4GVnEECElKztEgq9tHbFdYetu" ]
}
files = {
'data' : ( 'metadata' , json.dumps(para), 'application/json; charset=UTF-8' ),
'file' : ( 'application/zip' , open (i, "rb" ))
}
r = requests.post(
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 !!
Posts: 104
Threads: 36
Joined: Oct 2017
Posts: 2,342
Threads: 62
Joined: Sep 2016
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."
Posts: 104
Threads: 36
Joined: Oct 2017
(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.
1 2 3 4 5 6 |
authorization_url, state = flow.authorization_url(
access_type = 'offline' ,
include_granted_scopes = 'true' )
|
but i dont know how to use it can anyone explain ?
|