Python 3 PyDrive OAuth2 Script to Upload Image From URL to Google Drive in Terminal
Dec 2, 2025
Buy the full source code of application here:
https://procodestore.com/index.php/product/python-3-pydrive-oauth2-script-to-upload-image-from-url-to-google-drive-in-command-line/
Check the full description to see all Payment Links to Buy Source Code:
Buy the Source Code with PayPal (in USD):
https://www.paypal.com/ncp/payment/5AGGYJECQVZJC
Live Demo of App:
Buy the Source Code with Stripe (in INR):
https://buy.stripe.com/5kQ4gB8rCbcydjD4Vm14f1J
More Payment Options:
Watch My Visual Studio Code IDE Setup Video For Fonts,Themes & Extensions
Show More Show Less View Video Transcript
0:00
Uh hello friends. Today in this tutorial
0:02
I will be showing you that how basically
0:03
we can upload a image file from a URL to
0:06
Google drive automatically using a
0:08
Python script and we will be using
0:11
Pyrive library for making this process.
0:14
Let me show you a very simple example.
0:16
You can see this is the URL of the image
0:18
that we are uploading it. Let me run
0:21
this Python script here. And basically
0:23
it makes use of access token O2 flow. Uh
0:27
let me show you the drive here. This is
0:29
a Google drive here. No file is present
0:31
right here guys. So you can see no image
0:33
file is present. Let me just run this
0:35
Python script here. python app. py. So
0:38
what will happen guys? It will allows
0:40
you first of all you need to select your
0:42
account here. You need to select your
0:43
account. As you select the account here
0:45
after that the authorization flow is
0:47
completed. Now inside the console you
0:49
will see basically the file is returned
0:51
to us. Inside the JSON response we get
0:53
the four properties. the ID of the file,
0:56
name of the file and the name type which
0:58
is a JPG image. And if I check my if I
1:00
refresh the Google drive here guys, you
1:02
will see that you uploaded today. This
1:04
is the uh JPG file which is uploaded
1:07
right here from the URL to straight to
1:08
my Google Drive. So this is the Python
1:11
script guys which will actually upload
1:13
the image to Google Drive automatically.
1:15
Now let me show you how basically we can
1:17
achieve this. I have given all the
1:19
source code in the description of this
1:21
video guys. You can right click just go
1:23
to the description of this video and
1:24
copy paste all the source code. And now
1:27
first of all what we need to do is guys
1:28
we need to get this client secrets
1:31
dojson file which basically contains
1:34
your client ID, project ID, O URI, token
1:36
URI, client secret and redirect URI. So
1:40
these these things are very much
1:42
important guys. So I will show you step
1:44
by step how basically we can achieve
1:45
this. And uh now basically if you don't
1:50
provide this file here and if you just
1:52
execute this application you will get a
1:54
error something like this that invalid
1:57
client secret file. So you need to
1:58
create this f file the naming should be
2:02
same to same which is client secrets. So
2:05
the name should be same like this
2:08
and after this guys what we need to do
2:10
is that we need to go to our uh Google
2:13
cloud console here. Basically you need
2:15
to create a O2 client ID and simply this
2:18
will be a web application and these two
2:20
things are very much important. First is
2:22
the authorized JavaScript origins and
2:24
the authorized redirect URI and you need
2:26
to copy paste the same value that I'm
2:28
copy pasting here. This needs to be http
2:31
localhost_8080.
2:35
That's it. This is your authorized
2:36
JavaScript origins. Now inside your
2:38
authorized reader redirect URI this
2:41
needs to be HTTP localhost again port
2:44
number will be 8080 and then you need to
2:47
put the / symbol like like this and now
2:50
click on the create symbol. As you click
2:52
create here your O2 client ID will be
2:55
created. You will get your client ID
2:56
client secret. But you need to click
2:58
this third button which is download
3:00
JSON. So it will store all this file
3:02
inside your JSON file. So this is your
3:05
file here. You need to simply now cut
3:08
this file and simply paste it inside
3:10
your folder. So right here open this
3:12
here and simply you need to paste it
3:16
here
3:18
and uh
3:21
you need to basically rename this file
3:23
here to client
3:26
secrets. This is the naming that you
3:28
need to do same to same client secrets.
3:31
So now this is the file here guys.
3:33
Basically it contains the information
3:35
client ID, project ID, all this useful
3:37
information. So this will be different
3:39
for you. So just do this process first
3:41
of all. And now guys we need to go to
3:43
command line and install this library
3:45
which is called as pip install pi drive.
3:48
This is the library that we are using
3:50
for this project guys. This is the
3:52
library that you need to install. So
3:54
I've already installed this. So after
3:57
this guys we will first of all import a
3:59
module of o from this library. And here
4:02
we need to import Google O. This is the
4:05
module we need to import. And after this
4:07
we will import the input output module
4:10
as well. And uh
4:13
we will also import the request module
4:16
which will actually make a post request
4:18
to the Google drive API passing this
4:20
image file. And now you need to provide
4:22
the URL of the image guys. So whatever
4:24
URL that you are call uploading it. You
4:27
can just take any image from a internet.
4:30
Let's suppose I take a image of a flower
4:32
that I need to upload. So you can simply
4:35
copy image address. Simply go to here,
4:37
paste it here. You can take any image.
4:40
After this you can put a file name. So
4:43
this file name can be anything. Let's
4:44
suppose I give here flower
4:47
2. This is a file name. And here we can
4:50
even uh basically upload to a specific
4:53
folder guys. But here I will be
4:55
uploading to a root Google drive. This
4:58
will be basically not upload to a
5:00
specific folder. This will simply upload
5:02
to the Google drive. You can even paste
5:04
the folder ID as well here. After this
5:08
guys, what we need to do is we need to
5:10
instantiate a new instance of Google O.
5:12
So we will simply say call the new
5:15
constructor like this. And after this we
5:17
will set here guys, we will simply call
5:19
this local web server authentication. So
5:21
we will follow the local client side
5:23
authorization flow where we will
5:25
basically get the access token. After
5:28
this we need to set the metadata of the
5:30
image guys. Basically this will be a
5:32
simple JSON object which will contain
5:33
the two properties. First is the name of
5:36
the file which we have set here to file
5:38
name. And the second one is the parents.
5:40
Parents basically set is the folder ID
5:43
guys. So this time we have provided the
5:45
folder ID which is root here which will
5:47
actually upload it to the root of the
5:49
Google drive.
5:51
After setting these two options guys,
5:53
now we need to set our Google image
5:55
file. Whichever image file that we are
5:56
uploading, this will again be an object
5:58
and basically this will contain two
6:00
properties. First is the data and here
6:03
the data will be guys. First of all,
6:04
this takes the meta data property and
6:07
this needs to be here we just need to
6:11
import the JSON module as well. import
6:13
JSON. So this JSON module guys contains
6:17
a method called as dumps and uh dumps
6:21
method and inside this we will pass our
6:23
metadata that we have declared. You will
6:25
see we are passing this metadata. The
6:27
first one is the actual file name that
6:30
is flower 2 and basically the folder ID
6:33
is root. So we are just dumping this
6:35
JSON here. After this guys, we also need
6:38
to set a header which is called as
6:41
content type which is actually
6:43
application/json.
6:46
After this we need to put a comma and
6:47
then the second option is the actual
6:50
file that you are uploading to Google
6:52
drive. So for this we need to have the
6:54
input output
6:58
module of Python. This is a built-in
7:00
module
7:02
and basically it contains a method guys
7:04
which is called as bytes input output
7:08
and basically what it does guys it will
7:10
actually convert this image from the URL
7:12
that you have here this is the image
7:14
here you can see it has converted this
7:16
image
7:18
it will convert this image here to a
7:20
basically a bytes array so now what we
7:24
need to do is that here we need to
7:26
simply say request we We are importing
7:29
the request module here and we are
7:32
simply getting it from the URL. We will
7:34
pass the URL
7:36
and then we will simply call the content
7:39
method content we will get the actual
7:42
content from the URL of the image and
7:43
then we will convert it to a bytes array
7:46
and then we will be setting the file
7:47
here. That's all. And now we simply need
7:49
to make a post request guys to the
7:51
Google Drive API request. So there we
7:55
making a simple post request. So the
7:57
endpoint will be simply like this https
8:00
www.googleapis.com/upload/drive
8:06
version 3 and then we will say files
8:09
question mark upload type
8:14
and this is equal to multiart
8:18
put a comma here and after this we need
8:21
to set the headers. So headers would be
8:23
basically the authorization header guys.
8:25
Basically you just need to set the
8:27
header here authorization
8:30
uh basically this equals to colon here
8:33
and basically this value equal to beer b
8:35
e a r e r and then followed by some
8:38
space and then we will concatenate the
8:41
access token that we can get simply by g
8:45
credentials
8:47
and dot access token. That's it. access
8:51
token that's all. So again
8:55
basically this is an object here.
9:01
Okay I think this is an object. So you
9:04
need to put this inside curly brackets
9:06
like this. This is a JSON object. We are
9:09
passing it header here. So close it by
9:11
curly brackets. Put a comma.
9:17
Uh I add this
9:22
B access token. Oh sorry this needs to
9:28
just close it before curly bracket and
9:30
then
9:31
parenthesis and put a comma here and
9:34
this takes the third option which is the
9:36
actual body. So this will take the
9:38
actual files and this will be equal to
9:40
the files that we constructed earlier
9:42
on. You will see this is a files object
9:44
we are passing here. And if I just
9:46
format the document, you will see
9:48
basically it takes three options. This
9:50
request it first of all takes the URL
9:53
which URL you are making the request to.
9:55
Secondly, it takes the header object
9:57
which contains this authorization header
9:59
here. It can contains this access token.
10:01
Thirdly, it takes the body of the
10:03
request which is we are passing the
10:05
actual file up from the URL to upload it
10:08
to Google Drive. So now we can simply
10:10
upload this guys to Google Drive. Let me
10:13
run the script here. Python app.p py. So
10:16
first of all it will ask for the
10:18
permission. You need to select your
10:19
account. Authorization flow will be
10:21
completed. And now you will see that
10:24
basically
10:26
uh oh sorry we need to pass the URL. So
10:28
we have passed the B 64 code. So we need
10:30
to pass a URL here. Let me select this
10:32
image. So you can see it's a B 64 URL.
10:36
It's not a
10:38
URL of the image. It should be a URL
10:40
which is publicly accessible.
10:45
I think it's again so what I can do is
10:48
basically you can let me take this one
10:52
copy image address
10:55
yeah this is now a URL here which has
10:58
http attached to it so this is a
11:01
publicly URL
11:03
let me paste it here
11:07
so now if I run this once again
11:12
sorry let Let me select this account.
11:18
Let me stop this application.
11:27
Uh let me close this and start again.
11:49
Python app. py.
11:53
So authorization flow is completed and
11:55
basically you will see that if I refresh
11:57
the drive here.
12:01
So you will see that flower 2 is
12:03
successfully uploaded guys. You will see
12:05
this is a flower image which is
12:06
successfully uploaded to my Google drive
12:08
with the same name that I configured
12:10
right here. You see flower 2. So in this
12:12
way guys you can simply upload images
12:14
from URL to straight to your Google
12:16
drive using uh this pi drive library. Uh
12:19
basically it is using a simple post
12:21
request to the Google drive API. We are
12:23
passing the access token here itself.
12:25
You just need to construct this file
12:27
here client secrets.json JSON which
12:29
contain the information about your
12:30
project which will actually contain
12:32
client ID, secret, token URI, redirect
12:35
URI. I explained all this process in
12:38
this video. Please hit that like button,
12:40
subscribe the channel and I will be
12:41
seeing you in the next one.
