Articles, Blog

Google Drive SDK: Writing your first Drive app in PHP

November 5, 2019


MALE SPEAKER: Shanghai
GDG is a very interesting developer community. FEMALE SPEAKER: I’m
glad somebody has asked this question. MALE SPEAKER: This is where
the magic happens. FEMALE SPEAKER: This is
primarily a question and answer show. So if any of you out there would
like to ask questions– ERIC GILMORE: Welcome back to
Google Developers Live. I’m Eric Gilmore, a technical
writer with the Google Drive Developers Relation Team. And here with me today
is Claudio– CLAUDIO CHERUBINO: Hi. ERIC GILMORE: –Cherubino, an
engineer with the same team. And a fine engineer,
I might add. Recently, he’s done quite a bit
of work writing some new code samples that we’re
featuring in our public documentation. And we’d like to talk about one
of those today in detail. CLAUDIO CHERUBINO:
Thanks, Eric. So we recently– probably the beginning of
this week– we added a new page to our Docs. It’s a Quickstart page
on developers.googl e.com/drive/quickstart, which
will help you get started with the Drive API in, we think,
about 10 minutes. So you start from nothing. And you end up with a complete
command-line application in one of the languages we support,
which are Java, Python, PHP, .NET, and Ruby. And this application will
perform authorization for you and upload a file
to Google Drive. We have done another Google
Developers Live event last week, covering something
like this with Python. So if you want to learn about
this in Python, there’s a video in our library
that you can watch. Today, we’re going to do the
same thing by following these instructions but in PHP. So we have a list of videos we
can point them to, right? ERIC GILMORE: Sure. There’s a Drive YouTube playlist
with all of our recent videos, as well as some
videos from Google I/O that you could view– webinar, by one of our
developer advocates. So please, look at the playlist
and check it out. But specifically, if you’re
looking for the Python Quickstart, it’s described
there. It’s also described
in this page. This page covers all
of those languages. As Claudio said, today we’re
going to focus on PHP. But all languages are
covered here. CLAUDIO CHERUBINO: Yeah,
that’s true. And actually, whatever the
language of your choice, when you want to write this
command-line application, there are five steps
you have to follow. They are outlined in the Docs
and in this page we have in front of us. So the first step is to
enable the Drive API. Then we’re going to install the
Google Client Library for the programming language
that we chose. And set up the sample,
run the sample. And if you like that and you
want to do more, there is some suggestions on what
to do next. ERIC GILMORE: In case you were
wondering, Claudio writes the samples, then I write
this page. And then we argue about things
in order to give you this final polished product that
you’re looking at. CLAUDIO CHERUBINO: Yes. And it is polished. But maybe there’s still some
issues or is still something that is not clear. And if you have comments or
feedback on this page, please reach out to us on Google+. Or remember we monitor
Stack Overflow for Google Drive SDK questions. So if you have technical
questions, please post them on Stack Overflow. And we’ll be glad to answer
to those questions. ERIC GILMORE: If you’re looking
for very, very quick gratification, though, there’s
also a feedback link right on this page we’re looking at. CLAUDIO CHERUBINO: That’s
absolutely correct. Let’s see how to– oh, we’re
going to write command-line application in PHP. The first step is enabling
the Drive API. We need to create a Google
API project in the Google APIs console. And then enable the services
we need, and generate the credentials we need. Let’s do this step by step. So I open the Google
APIs console. I have no projects. This is the first time I create
a new project, which is probably what you will see if
this is the first time you try writing a Google API project. And I have this big button
that says Create Project. And this is what I’m
going to click. So when I do, I go to this page,
where I see the list of services that I can enable and
disable for my application. And in our sample, we need to
enable the Drive API service. But if you need to use the Drive
API and other services, let’s say the Calendar API, you
can enable both from here. You can enable as many APIs
as you need from here. So I’ll scroll down, find Drive
API, and enable it by clicking on the on-off switch. I also see that I have a limit
of 500,000 requests per day. This should be enough for
our sample today. ERIC GILMORE: That’s
a generous quota. CLAUDIO CHERUBINO:
Yeah, it is. I enabled my service. I need to generate
my credentials. The Drive API uses OAuth2 as the
authorization mechanism. And in order for the auth flow
to run, you need client ID and client secret. ERIC GILMORE: Do all requests
to the API need to be authorized? Is that still true? CLAUDIO CHERUBINO: So there’s
some cases where you may want to publish a document to
the general public. And so you want anyone without
any credentials to be able to access that. Let’s say it’s a special case. But in general, almost all API
calls require authentication. ERIC GILMORE: Right. CLAUDIO CHERUBINO: So I said
I want to create my new credentials, OAuth2
credentials. So I click on API Access in
the left navigation menu. And I have no credentials,
so I have to create an OAuth 2.0 client ID. It’s a step by step process. I need to specify
a product name. This can be name of a product. I’m going to say Drive PHP. Maybe you’re going to choose
something that is more descriptive. And if you have a product logo,
you can add it here. You may add it later if you
are just testing now. And then when you’re ready to
launch a product, you may want to add a logo and description,
a lot of other things. But this time, we don’t
need a logo. So let’s click on Next. And then we have to choose if we
want our application to be a web application, a service
account application, or an installed application. Usually, you’re probably going
to select a web application. But today, we are going to
write a command-line application, which is an
installed application. It’s called an installed
application. ERIC GILMORE: And this is
because our goal was to get started quickly. CLAUDIO CHERUBINO: Yes. So we will see at the end of
this talk that the user experience for a command-line
application is not great. User experience when
you have a web application is much nicer. But for the sake of example,
this is probably a better way to get started. And all we say about the
Drive API usage is going to be the same. And then you have to build an
application on top of it. But all you learn from here
can be used in a web application. ERIC GILMORE: That’s right. CLAUDIO CHERUBINO: So let’s
say Installed Application. And then this is not Android. This is not iOS. It’s Other, which is a
command-line application running on my computer. I click on Create Client ID. And what I get is a new box
titled Client ID for Installed Applications, which includes
client ID and client secret. The client secret, as the name
says, must be secret. So you should make sure that
you never share this client secret with anyone. ERIC GILMORE: For instance,
not on the public internet on a video. CLAUDIO CHERUBINO: Oh, yeah. Yeah, that’s a good idea. So let’s say you’re not
smart, so like me. And you showed your client
secret on a public video. And you don’t want other
people to use your credentials. What do you do? At the end of this talk, you go
back to the APIs console. Click under Set Client Secret,
and this current client secret will be revoked, so
no longer valid. And you get a new one that you
can use in your application. The client ID will
stay the same. So you only need to replace
the client secret. You can consider client ID
and client secret as username and password. You might want your username to
be known, but you’d never want your password to be
known to other people. ERIC GILMORE: Right. CLAUDIO CHERUBINO: So with this,
we go back to the Docs and see that we’ve done
the first step, enable the Drive API. We can move on to the second
step, which is installing the client library for the language
of your choice. I selected Python for today. And to install Python– to install PHP, sorry. I selected PHP for today. I selected Python last week. This is what I was
trying to say. And for PHP, the instructions
are very easy. There’s just a one-line command
that we can run to check out the source from
the public repository. So I can take this line– ERIC GILMORE: In Python’s
defense, its setup is easy, too. CLAUDIO CHERUBINO: Yes, it’s
still on one line. So I can take that line and
run it on my command line. And what I get is the source
for a Google API client library for PHP. And if I go to the folder, I see
that I have a Google API PHP client folder, which
contains what we just got, and then two more files. We are going to see right
now what they’re for. ERIC GILMORE: Incidentally,
the PHP client recently underwent a significant
refactoring. So you might see some
improvements there. If you have an older copy,
you might want to upgrade that copy. CLAUDIO CHERUBINO: Yeah,
that’s 100% correct. And we posted this news
in our Change Log. So remember, every time we add
something on the Docs or in the libraries or anything that
is related to the Drive SDK, we update the release notes
in our documentation. So this was just added
a couple days ago. OK. Let’s go back to Docs and see
what we have to do next. So we’ve done step
two, install the Google client library. It’s time to do step three,
set up the sample. To run this sample, we
need two files– the source file in PHP and the
document to be uploaded into Google Drive. I created the text document. So I called it document.txt. And it’s a text document with
some sample content, friendly sample content. This is the file we’re going
to upload to Google Drive. And then I also have
a drive.php file, which is empty. So if you want to get started
quickly, you can just go to this page, take this code, and
copy it into your file. And then we will try it. And then I will explain line
by line what’s happening on your computer. So let me copy that
into my file. Let’s make this bigger. So let’s see these lines. First, we require the Google
client library. And the Google client
library is in google-api-php-client/src. So we just downloaded it. So if you download it as we
did, this is going to be exactly the correct path. Next, we have to set
our credentials from the APIs console. So the sample code obviously
will have some place holders. And you have to replace your
client ID or your client secret with the values you got
from the APIs console. So I’m going to do that. Let me go back to the
APIs console. I can take my client ID
and copy it here. And then I can do the
same with my client secret, and put it there. And this is the only edit you
need to do to get started. If you want to add new features,
then of course you have to write them. But if you want to get started,
that’s the only thing you have to do. Everything else is
already working. I’ll say, before we go and
explain line by line, should we run this and see
what happens? ERIC GILMORE: Absolutely. CLAUDIO CHERUBINO: OK. ERIC GILMORE: Can I ask one
question, though, first? CLAUDIO CHERUBINO: Sure. ERIC GILMORE: The redirect URI
that’s given there– is that a standard URI for installed
applications? CLAUDIO CHERUBINO: Yes. So this is a standard URI that
is used for installed applications. When you have a web application,
you have to specify your own. But with installed applications,
this is going to be the standard value. It’s provided by– if you see in the APIs console,
you will find the same redirect URI. And you don’t need to change
it if you use installed applications. And one minute– while we’re
talking here, we’re talking about the scopes. The auth scopes are basically
what we’re asking the user for, what permissions we want. And in our case, we are asking
for the full Drive permission. So with this sample, we are
asking to have access to all the documents for the user
who runs the sample. There are different scopes. Even just for the Drive API,
there are different scopes you can use. So you don’t need to ask for
the full Drive access. We actually don’t
recommend it. ERIC GILMORE: Exactly. Our general recommendation is to
ask for the most specific, limited scope that you can
request, and still have your application work. CLAUDIO CHERUBINO: Yeah. But this is a sample. And of course, you
can tweak that. So let me run this. I go back to my terminal. If I just do PHP and my file
is called drive.php, so I’m running it from the command
line, the first thing the application does, it says,
please visit this URL. So the auth flow requires the
user to grant access to the application from a browser. But our command-line application
has no browser. So what it can do is that it
shows you the URL to visit. And then waits for you
to do something. So this is what I was referring
to when I said the user experience is
not the greatest. The user, in a command-line
application, has to copy the link, go to his browser, visit
that page, and will find the famous OAuth2 flow dialogue. Which says that the drive.php,
which is the application I just created, is requesting
permission to view and manage files and documents in
my Google Drive. And if I want to know more
details, I can expand it and see more details. I know this application,
so I’m going to allow access to it. And what I get, since this is
an installed application, is that I get back a code that I
have to copy and paste into the command line. Once I enter this code, the
application will be authorized. And the next thing it will try
to do is upload that file to Drive, and then print
all the metadata we get back from Drive. So I insert my authorization
code. And I get a lot of things. It’s JSON structure. That is exactly what we
get back from Drive. Of course, this is an example. So you probably don’t want to
just dump everything you get back from the API
to the console. You probably want to
do something else. But still, this is useful for
you to learn what you can see, what you get from
the Drive API. So we uploaded a file. And we get its ID here. We get a link to it. We get some other links to it. We can talk about it. We get a title– what type the document is, the
description, and so on. A lot of flags, a
lot of labels. The document has everything
you might need about this document. ERIC GILMORE: I think for the
purposes of our sample, it’s interesting to note that the
title is what you’ll look for when you go to the Drive
UI, not the file name. CLAUDIO CHERUBINO: Yes. Let’s check it out. So if I go to my Google Drive, a
document called My Document. We’ll see why this document
is called My Document. But a document called
My Document has just been uploaded. And if I open it, it’s
just a text file. So the Google Viewer will be
able to open it and will just show the content. And say hi, I’m a
test document. So it worked. Let’s see how that worked. Let’s go back to the code. So if you remember, the first
thing we did was to show the user an authorization URL. This is done by using
the client library. You don’t have to do
much to do it. You just ask the client, which
is the one we sent our credentials to, and
say client, create authorization URL. And then we take this
authorization URL and send it to the user. And ask him to visit this URL. Once the user insert the
authorization code, we store it in another variable called
authorization code. We ask the client library to
perform the authorization flow and authenticate it
with this code. And once that is done, we get an
access token, which is what you need to send request. So I’m not going into details
about the OAuth2 flow. But when you perform
authorization, you get two types of tokens, refresh tokens
and access tokens. Access tokens are short lived
and are needed to send requests, authorize requests. Refresh tokens shouldn’t be sent
with your request, but can be used to regenerate
new access tokens when you need them. So we have a good recommendation
about it. I’m not going to explain
it here. If you want to know more,
there’s a good page for it. So that’s everything we need
to do, authorization. Now our client has
a valid access token, can send request. So what we want to do next
is to insert a file. To insert a file, we have to
specify the metadata, such as the title, description, and MIME
type, and the contents of the file we want to upload. So in our case, this
sample hardcodes some of those values. That’s why, for instance, we set
the title to always be My Document [? in this ?] line. That means that wherever a
document is, in Google Drive, this is going to be called
My Document. Of course, when you write your
real application, you want this title to be a more
descriptive one. So for instance, you can take
the file name from the file you’re uploading and using that,
or any other thing that makes the file unique. Then you can add a description,
which is in our case hardcoded. But of course, you should
provide a better description. And we also specify
a MIME type. So this sample application
always uploads text files. So we hardcoded that. But remember, you might want to
upload images or any other kinds of file. And in that case, the MIME
type should match what your file is. ERIC GILMORE: If you’re like me,
after you run this sample for the first time, you’ll go
back, change some of these hardcoded values just
to make sure that it’s actually running. CLAUDIO CHERUBINO: OK. Yeah. ERIC GILMORE: You don’t
have to do that. CLAUDIO CHERUBINO: You only do
that if you don’t trust me, and he doesn’t. ERIC GILMORE: Fortunately,
it worked. CLAUDIO CHERUBINO: Yeah. Whoa. So I said we need metadata,
but we also need the file content, which is the string,
hi, I’m a test document. And we do that by using a PHP
method, file_get_contents, which reads a document,
a file, into a string. So data will contain all
our content, whatever that content is. And then we actually send a
request, which is a one line. We have this service class,
which is a Drive service. It has a list of resources,
including Files, which is what we want. And for each resource,
we have methods. And for instance, Files has
Insert, which is used to create a new file. And to insert, you pass the
file, which is the metadata we just created, and the rest of
the data, such as the content and the MIME type. That’s it. When you send that request, the
variable created file will contain the response from the
API, which is an object or adjacent structure,
in our case. And as you remember, we just
dumped this content to the command line. And that’s what the
last line does. Easy, isn’t it? And with that, we can go back
to the Docs, and see that we will have done step three
and step four. So set up the sample
and ran the sample. All it takes now is you
to make this better. So the sample works. It uploads a file. But there might be other
things you want to do. For instance? ERIC GILMORE: Oh, you might
want to list files. CLAUDIO CHERUBINO: List
files, for instance. If you want to do that,
it’s very easy. I’m not going to do it now. But remember, you had a call
Service, Files, Insert. There is another call, which
is Service, Files, List. Or if you want to get a specific
file, you have Service, Files, Get,
and so on. All of the other requests you
can do are documented in our reference guide. So you can see that you can
manage files, you can manage permissions, you can manage
revisions, and so on. But probably one of the other
things you might want to do is build a web application. And perhaps integrate your
application with the Drive UI. So the users can click on a file
from Drive and land at your application. All of this is supported. And all of this can be done
with the Drive SDK. We have documentation for it. And if you start from this
Quickstart page, at the end, you will see links to all these
things we mentioned. So that you can go directly
there and make your application better. ERIC GILMORE: Sure. And if you’ve got a lot of
energy and courage, you could also go and review the
authorization documentation. I’m not going to lie and tell
you that it’s easy, but it is interesting. And we have a link
to that, as well. CLAUDIO CHERUBINO: Definitely,
it is not the easiest part. But because it has to be
secure, so we have to– this is a security standard. OAuth is a standard. We have to make sure that, of
course, your credentials are not stolen, that you are who you
are, and you’re allowed to do what you want to do. The good thing is that the
libraries do all the heavy lifting for you. So I don’t really have to know
how the auth flow works if I use the library. As you’ve seen, I have a couple
of instructions to run, and that’s it. So it is definitely interesting
to know. And you should know about OAuth
to handle all the edge cases, all the special
cases, and so on. But to get started– you
can start without that. Before we wrap up, I
will just say that this is a first sample. But we have more complex
samples in our Docs. So this page allows you to get
started in five minutes. But if you want to write a real
web application that does a lot more, we have a section
on example application. Those are complete sample
applications in the same file languages. So we have Java, Python,
PHP, .NET, and Ruby. And these applications show how
you can build a complete web app that integrates with
the Drive UI that manages authentication and all the
edge cases, and basically everything. So let’s say you want to do
PHP, because today we’re talking about PHP. We have this application
called DrEdit– or “Dred-it,” we still
haven’t decided– which is a text editor
for Google Drive. Obviously, it’s a
longer document. There are more steps
to get started. But you can learn about
it step by step. And you can also download the
source so that you can try it on your computer. I would definitely recommend
you to check this out. ERIC GILMORE: Absolutely. CLAUDIO CHERUBINO: Cool. And with that, I think we should
wrap up for today. I hope this was useful. And if you have questions,
please reach out to us as soon as possible. ERIC GILMORE: Just to remind
you, the feedback link in the documentation should get you a
quick response to anything that you find in the
documentation that you’d like to comment on. And Stack Overflow is constantly
monitored by our engineers, as well. CLAUDIO CHERUBINO: Cool. Perfect. Thank you, and see
you next time.

6 Comments

  • Reply Zeren John January 13, 2014 at 5:05 am

    Why are most of the comments from a year ago, and then nothing?  Dead.  No activity?  And you state that the command line example was given to get up us and running quickly, BUT we will usually use the Web Application method.  So you won't show us the Web Application method.  I want to know how to use the method that I'll probably use.  If it takes a little longer, I won't mind.

  • Reply Alan Wells January 16, 2014 at 11:00 pm

    In Windows, in the command line window, you can NOT copy the authorization URL.  That means that you would need to type everything in manually.  That will take you a LONG time, and you'll probably get it wrong.

  • Reply Alan Wells January 17, 2014 at 2:41 am

    If you are using Google App Engine to host your website, you may see options for:
    Compute Engine and App Engine
    Client ID for native application
    Client ID for web application
    Service Account
    If you create a new Client ID, and choose the `Installed application` option, it names it:  `Client ID for native application`.  STUPID!!  Name it what you call it!  If I'm looking to create a Client ID for an installed application, but I see no option for an installed application, then what do you do?  If Google wants to intentionally create confusion, for people who might not know the terms are interchangeable, then your doing an awesome job.

  • Reply Alan Wells January 17, 2014 at 4:53 am

    There is a way to paste text into a windows operating system command line.  In the upper most left hand corner, click the icon, choose Properties, then the `Options` tab, then in the `Edit Options` frame, enable the `Quick Edit Mode`.  Copy as usual, BUT paste by RIGHT CLICKING on the command line.

  • Reply Nicolas Malvasio November 21, 2014 at 10:51 pm

    Hello, if I upload a file from my computer using a form made in php, the file goes througt my server and then to google drive, or the upload is directly to google drive. I ask you this because the use of resources.

    Thank you

  • Reply Andy Knudsen September 8, 2017 at 4:10 am

    Why demo something even the Devs say is a poor application?

  • Leave a Reply