{"id":2512,"date":"2021-11-02T16:49:00","date_gmt":"2021-11-02T21:49:00","guid":{"rendered":"https:\/\/xlinesoft.com\/blog\/?p=2512"},"modified":"2026-08-16T15:58:16","modified_gmt":"2026-08-16T20:58:16","slug":"working-with-google-drive-rest-api","status":"publish","type":"post","link":"https:\/\/xlinesoft.com\/blog\/2021\/11\/02\/working-with-google-drive-rest-api\/","title":{"rendered":"Connect PHPRunner to Google Drive with the REST API"},"content":{"rendered":"<p>This tutorial shows how to retrieve a list of files from Google Drive through the Google Drive REST API and display those files in a PHPRunner or ASPRunner.NET application.<\/p>\n<p>The example focuses on listing and viewing files. The same REST API can also be used for other Google Drive operations when the appropriate permissions are granted.<\/p>\n<p><a href=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/10\/googledrive_results.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/10\/googledrive_results-600x391.png\" alt=\"\" width=\"600\" height=\"391\" class=\"alignnone size-medium wp-image-2519\" srcset=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/10\/googledrive_results-600x391.png 600w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/10\/googledrive_results-768x500.png 768w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/10\/googledrive_results-1024x667.png 1024w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/10\/googledrive_results.png 1227w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><br \/>\n<!--more--><\/p>\n<p><strong>Update note:<\/strong> The Google OAuth setup in this article has been updated. Google Drive still uses OAuth 2.0, but the Google Cloud configuration and scope guidance have changed since the original article was published.<\/p>\n<h3>1. Create a Google Cloud project and enable the Drive API<\/h3>\n<p>Proceed to the <a href=\"https:\/\/console.cloud.google.com\/\">Google Cloud Console<\/a> and create a new project or select an existing one.<\/p>\n<p>Enable the <strong>Google Drive API<\/strong> for that project.<\/p>\n<p>You can find it under APIs &amp; Services by searching for Google Drive API and clicking <strong>Enable<\/strong>.<\/p>\n<p><a href=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/10\/googledrive_enableapi.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/10\/googledrive_enableapi-600x339.png\" alt=\"\" width=\"600\" height=\"339\" class=\"alignnone size-medium wp-image-2525\" srcset=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/10\/googledrive_enableapi-600x339.png 600w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/10\/googledrive_enableapi.png 730w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p>The screenshot above shows an earlier version of the Google Cloud interface. The exact menu layout may change, but the required step is still to enable the Google Drive API in your project.<\/p>\n<h3>2. Configure OAuth consent and Drive permissions<\/h3>\n<p>Configure the OAuth settings for your application in Google Cloud.<\/p>\n<p>Depending on the current Google Cloud interface, these settings may appear under <strong>Google Auth Platform<\/strong> and include sections such as Branding, Audience and Data Access.<\/p>\n<p>Set up the application name and support information, choose the appropriate audience, and add the Google Drive scope required by the application.<\/p>\n<p>For this tutorial we only need to retrieve file metadata and display links to files, so use:<\/p>\n<div class=\"my-syntax-highlighter\">\n<pre><textarea id=\"mshighlighter\" class=\"mshighlighter\" language=\"\" name=\"mshighlighter\" >\r\nhttps:\/\/www.googleapis.com\/auth\/drive.metadata.readonly<\/textarea><\/pre>\n<\/div>\n<p>If your application needs to download the contents of all files, you may need a broader scope such as:<\/p>\n<div class=\"my-syntax-highlighter\">\n<pre><textarea id=\"mshighlighter\" class=\"mshighlighter\" language=\"\" name=\"mshighlighter\" >\r\nhttps:\/\/www.googleapis.com\/auth\/drive.readonly<\/textarea><\/pre>\n<\/div>\n<p>Do not request broader access unless the application actually needs it.<\/p>\n<p>If your OAuth application is still in <strong>Testing<\/strong> mode and uses an External audience, add the Google accounts that will test the application as test users.<\/p>\n<p>For current information about Drive scopes, see the <a href=\"https:\/\/developers.google.com\/workspace\/drive\/api\/guides\/api-specific-auth\">Google Drive API authorization documentation<\/a>.<\/p>\n<h3>3. Create OAuth client credentials<\/h3>\n<p>Create an OAuth client for the project and choose <strong>Web application<\/strong> as the application type.<\/p>\n<p><a href=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/10\/googledrive_apikeys.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/10\/googledrive_apikeys-600x325.png\" alt=\"\" width=\"600\" height=\"325\" class=\"alignnone size-medium wp-image-2523\" srcset=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/10\/googledrive_apikeys-600x325.png 600w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/10\/googledrive_apikeys-768x416.png 768w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/10\/googledrive_apikeys-1024x555.png 1024w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/10\/googledrive_apikeys.png 1920w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p><a href=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/10\/googledrive_oauthclient.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/10\/googledrive_oauthclient.png\" alt=\"\" width=\"510\" height=\"479\" class=\"alignnone size-full wp-image-2524\" \/><\/a><\/p>\n<p>These screenshots show the older Google Cloud interface, but the information you need is the same: create a Web application OAuth client and obtain its <strong>Client ID<\/strong> and <strong>Client Secret<\/strong>.<\/p>\n<p>Add an <strong>Authorized redirect URI<\/strong> that points back to your generated application.<\/p>\n<p>For local PHPRunner testing:<\/p>\n<div class=\"my-syntax-highlighter\">\n<pre><textarea id=\"mshighlighter\" class=\"mshighlighter\" language=\"\" name=\"mshighlighter\" >\r\nhttp:\/\/localhost:8086\/oauthcallback.php<\/textarea><\/pre>\n<\/div>\n<p>For local ASPRunner.NET testing:<\/p>\n<div class=\"my-syntax-highlighter\">\n<pre><textarea id=\"mshighlighter\" class=\"mshighlighter\" language=\"\" name=\"mshighlighter\" >\r\nhttp:\/\/localhost:8086\/oauthcallback<\/textarea><\/pre>\n<\/div>\n<p>For a published PHPRunner application:<\/p>\n<div class=\"my-syntax-highlighter\">\n<pre><textarea id=\"mshighlighter\" class=\"mshighlighter\" language=\"\" name=\"mshighlighter\" >\r\nhttps:\/\/yourwebsite.com\/project\/oauthcallback.php<\/textarea><\/pre>\n<\/div>\n<p>For a published ASPRunner.NET application:<\/p>\n<div class=\"my-syntax-highlighter\">\n<pre><textarea id=\"mshighlighter\" class=\"mshighlighter\" language=\"\" name=\"mshighlighter\" >\r\nhttps:\/\/yourwebsite.com\/project\/oauthcallback<\/textarea><\/pre>\n<\/div>\n<p>Replace <strong>yourwebsite.com\/project<\/strong> with the actual URL of your application.<\/p>\n<p>The redirect URI entered in Google Cloud must match the application callback URL exactly.<\/p>\n<h3>4. Create the REST API connection<\/h3>\n<p>In PHPRunner or ASPRunner.NET, add a new REST API connection and choose <strong>OAuth 2.0 &#8211; User<\/strong> as the authentication method.<\/p>\n<p><a href=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/10\/googledrive_restconnection.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/10\/googledrive_restconnection-600x466.png\" alt=\"\" width=\"600\" height=\"466\" class=\"alignnone size-medium wp-image-2520\" srcset=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/10\/googledrive_restconnection-600x466.png 600w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/10\/googledrive_restconnection-768x597.png 768w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/10\/googledrive_restconnection.png 1005w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p>Use this REST API URL:<\/p>\n<div class=\"my-syntax-highlighter\">\n<pre><textarea id=\"mshighlighter\" class=\"mshighlighter\" language=\"\" name=\"mshighlighter\" >\r\nhttps:\/\/www.googleapis.com\/drive\/v3<\/textarea><\/pre>\n<\/div>\n<p>Enter the <strong>Client ID<\/strong> and <strong>Client Secret<\/strong> created in Google Cloud.<\/p>\n<p>Use the following OAuth settings.<\/p>\n<p><strong>Authorization URI<\/strong><\/p>\n<div class=\"my-syntax-highlighter\">\n<pre><textarea id=\"mshighlighter\" class=\"mshighlighter\" language=\"\" name=\"mshighlighter\" >\r\nhttps:\/\/accounts.google.com\/o\/oauth2\/v2\/auth<\/textarea><\/pre>\n<\/div>\n<p><strong>Access token URI<\/strong><\/p>\n<div class=\"my-syntax-highlighter\">\n<pre><textarea id=\"mshighlighter\" class=\"mshighlighter\" language=\"\" name=\"mshighlighter\" >\r\nhttps:\/\/oauth2.googleapis.com\/token<\/textarea><\/pre>\n<\/div>\n<p><strong>Scope<\/strong><\/p>\n<div class=\"my-syntax-highlighter\">\n<pre><textarea id=\"mshighlighter\" class=\"mshighlighter\" language=\"\" name=\"mshighlighter\" >\r\nhttps:\/\/www.googleapis.com\/auth\/drive.metadata.readonly<\/textarea><\/pre>\n<\/div>\n<p>For more information about REST API connections, see the <a href=\"https:\/\/xlinesoft.com\/phprunner\/docs\/rest-api-connections.htm\">PHPRunner REST API Connections documentation<\/a> or the <a href=\"https:\/\/xlinesoft.com\/asprunnernet\/docs\/rest-api-connections.htm\">ASPRunner.NET REST API Connections documentation<\/a>.<\/p>\n<h3>5. Enable the List operation<\/h3>\n<p>The Google Drive API v3 endpoint for listing files is:<\/p>\n<div class=\"my-syntax-highlighter\">\n<pre><textarea id=\"mshighlighter\" class=\"mshighlighter\" language=\"\" name=\"mshighlighter\" >\r\nGET https:\/\/www.googleapis.com\/drive\/v3\/files<\/textarea><\/pre>\n<\/div>\n<p>In the REST View, enable the <strong>List<\/strong> operation.<\/p>\n<p><a href=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/10\/googledrive_list.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/10\/googledrive_list-600x423.png\" alt=\"\" width=\"600\" height=\"423\" class=\"alignnone size-medium wp-image-2521\" srcset=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/10\/googledrive_list-600x423.png 600w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/10\/googledrive_list-768x541.png 768w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/10\/googledrive_list-1024x722.png 1024w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/10\/googledrive_list.png 1176w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p>To preserve the original example&#8217;s All Fields search, use the following Resource:<\/p>\n<div class=\"my-syntax-highlighter\">\n<pre><textarea id=\"mshighlighter\" class=\"mshighlighter\" language=\"\" name=\"mshighlighter\" >\r\n\/files?<?q=name%20contains%20':{all_field_search}'?><\/textarea><\/pre>\n<\/div>\n<p>The <strong>q<\/strong> parameter is the Google Drive API search expression. In this example, the generated application&#8217;s All Fields search is used to find files whose names contain the search text.<\/p>\n<h3>6. Add the fields you want to display<\/h3>\n<p>With OAuth 2.0 &#8211; User connections, define the response fields used by your REST View.<\/p>\n<p><a href=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/11\/googledrive_fields.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/11\/googledrive_fields-600x300.png\" alt=\"\" width=\"600\" height=\"300\" class=\"alignnone size-medium wp-image-2534\" srcset=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/11\/googledrive_fields-600x300.png 600w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/11\/googledrive_fields-768x384.png 768w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/11\/googledrive_fields-1024x512.png 1024w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/11\/googledrive_fields.png 1169w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p>Useful Google Drive file metadata fields for this example include:<\/p>\n<ul>\n<li>id<\/li>\n<li>name<\/li>\n<li>description<\/li>\n<li>mimeType<\/li>\n<li>trashed<\/li>\n<li>starred<\/li>\n<li>createdTime<\/li>\n<li>modifiedTime<\/li>\n<li>webViewLink<\/li>\n<\/ul>\n<p>The <strong>id<\/strong> value uniquely identifies the Google Drive file. The <strong>webViewLink<\/strong> value can be used to open the file in the appropriate Google Drive viewer or editor.<\/p>\n<p>Google Drive supports many additional File resource fields, so add only the fields your application actually needs.<\/p>\n<h3>7. Enable the Single operation<\/h3>\n<p>Enable the Single operation to retrieve one file by its Google Drive file ID.<\/p>\n<p><a href=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/10\/googledrive_single.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/10\/googledrive_single-600x448.png\" alt=\"\" width=\"600\" height=\"448\" class=\"alignnone size-medium wp-image-2522\" srcset=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/10\/googledrive_single-600x448.png 600w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/10\/googledrive_single-768x573.png 768w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/10\/googledrive_single-1024x764.png 1024w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2021\/10\/googledrive_single.png 1025w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p>Use:<\/p>\n<div class=\"my-syntax-highlighter\">\n<pre><textarea id=\"mshighlighter\" class=\"mshighlighter\" language=\"\" name=\"mshighlighter\" >\r\n\/files\/:{filter.id}?fields=*<\/textarea><\/pre>\n<\/div>\n<p>For a production application you can also request only the fields you need instead of using <strong>fields=*<\/strong>.<\/p>\n<p>For example:<\/p>\n<div class=\"my-syntax-highlighter\">\n<pre><textarea id=\"mshighlighter\" class=\"mshighlighter\" language=\"\" name=\"mshighlighter\" >\r\n\/files\/:{filter.id}?fields=id,name,description,mimeType,createdTime,modifiedTime,webViewLink<\/textarea><\/pre>\n<\/div>\n<h3>Troubleshooting OAuth<\/h3>\n<p>If Google reports a <strong>redirect_uri_mismatch<\/strong> error, compare the callback URL generated by your application with the Authorized redirect URI configured in Google Cloud. They must match exactly.<\/p>\n<p>If an application in Testing mode does not allow a user to authenticate, make sure that Google account has been added as a test user.<\/p>\n<p>Google OAuth access tokens are short-lived. Long-running OAuth integrations use refresh tokens to obtain new access tokens without asking the user to authenticate for every request.<\/p>\n<p>Also remember that Google treats several Drive-wide scopes, including <strong>drive.metadata.readonly<\/strong> and <strong>drive.readonly<\/strong>, as restricted scopes. Public applications that use these scopes may be subject to Google&#8217;s OAuth verification requirements.<\/p>\n<p>For current Google documentation, see the <a href=\"https:\/\/developers.google.com\/workspace\/drive\/api\/guides\/api-specific-auth\">Google Drive authorization guide<\/a>, the <a href=\"https:\/\/developers.google.com\/identity\/protocols\/oauth2\/web-server\">OAuth 2.0 Web Server guide<\/a> and the <a href=\"https:\/\/developers.google.com\/workspace\/drive\/api\/reference\/rest\/v3\/files\/list\">Drive API v3 files.list reference<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to retrieve a list of files from Google Drive through the Google Drive REST API and display those files in a PHPRunner or ASPRunner.NET application. The example focuses on listing and viewing files. The same REST API can also be used for other Google Drive operations when the appropriate permissions are granted.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,1,8],"tags":[],"class_list":["post-2512","post","type-post","status-publish","format-standard","hentry","category-asp-net","category-php-category","category-tutorials"],"_links":{"self":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts\/2512","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/comments?post=2512"}],"version-history":[{"count":17,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts\/2512\/revisions"}],"predecessor-version":[{"id":3445,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts\/2512\/revisions\/3445"}],"wp:attachment":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/media?parent=2512"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/categories?post=2512"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/tags?post=2512"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}