{"id":5174,"date":"2021-07-01T22:52:51","date_gmt":"2021-07-01T22:52:51","guid":{"rendered":"https:\/\/pariswells.com\/blog\/?p=5174"},"modified":"2021-07-20T06:48:40","modified_gmt":"2021-07-20T06:48:40","slug":"google-drive-api-credential-json-and-token-json-access-token","status":"publish","type":"post","link":"https:\/\/pariswells.com\/blog\/research\/google-drive-api-credential-json-and-token-json-access-token","title":{"rendered":"Google Drive API \\ Credential.json and token.json ( Access Token )"},"content":{"rendered":"<p>Recently had to reverse engineer a Google Drive PHP API Script, and I thought I would document the process!<\/p><p>Before you go down the below path , you will need to make sure you can push your project <a href=\"https:\/\/support.google.com\/cloud\/answer\/10311615#publishing-status&amp;zippy=%2Ctesting%2Cinternal%2Cexternal\">In Production <\/a>(Very Long winded )<\/p><p>Testing mode for your project (even in Offline mode ) the below Refresh and Access Tokens with Expire every 7 days , if not use <a href=\"https:\/\/pariswells.com\/blog\/research\/how-to-use-a-service-account-to-access-google-drive-api-instead-of\">service account<\/a><\/p><p><strong>Credentials.JSON<\/strong><\/p><p>Format of this file is like below<\/p><pre>{\"web\":{\"client_id\":\"xxxxxxxxxxxx\",\"project_id\":\"xxxxxxxxxxx\",\"auth_uri\":\"https:\/\/accounts.google.com\/o\/oauth2\/auth\",\"token_uri\":\"https:\/\/oauth2.googleapis.com\/token\",\"auth_provider_x509_cert_url\":\"https:\/\/www.googleapis.com\/oauth2\/v1\/certs\",\"client_secret\":\"xxxxxxxxxxxxxxxx\",\"redirect_uris\":[\"xxxxxxxxxxxxxx\",\"xxxxxxxxxx\"]}}<\/pre><p><span style=\"text-decoration: underline;\"><strong>How do get this\u00a0<\/strong><\/span><\/p><ol><li>Create a new google account for Google drive and enable that account for API access ( https:\/\/developers.google.com\/drive\/api\/v3\/enable-drive-api )<\/li><li>Create OAuth client ID and Select Web Application , add the authorize URLs and create\u00a0<\/li><li>Download Crdential.json of the client your just created<p id=\"XKKIQLJ\"><img loading=\"lazy\" decoding=\"async\" width=\"1479\" height=\"56\" class=\"alignnone size-full wp-image-5178  img-responsive\" src=\"https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2021\/07\/img_60de41a89cb73.png\" alt=\"\" srcset=\"https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2021\/07\/img_60de41a89cb73.png 1479w, https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2021\/07\/img_60de41a89cb73-300x11.png 300w, https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2021\/07\/img_60de41a89cb73-1024x39.png 1024w, https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2021\/07\/img_60de41a89cb73-768x29.png 768w\" sizes=\"auto, (max-width: 1479px) 100vw, 1479px\" \/><\/p><\/li><\/ol><p>&nbsp;<\/p><p><strong>Token.JSON<\/strong><\/p><p>This is what they can your Access Token , Format of this file is like below<\/p><pre>{\"access_token\":\"xxxxxxxxxxxxxxxxxx\",\"expires_in\":3599,\"scope\":\"https:\\\/\\\/www.googleapis.com\\\/auth\\\/drive.metadata.readonly\",\"token_type\":\"Bearer\",\"created\":1625147606,\"refresh_token\":\"xxxxxxxxxxxxxxxxxxxxxx\"}<\/pre><p>The script used the existing code here <a href=\"https:\/\/developers.google.com\/docs\/api\/quickstart\/php\">https:\/\/developers.google.com\/docs\/api\/quickstart\/php<\/a><\/p><p>You can see that the script is supposed to retrieve the token.json file when you give it the crdential file, however when I ran this I got\u00a0<\/p><p class=\"lang-php s-code-block\"><code class=\"hljs language-php\">Notice: <span class=\"hljs-keyword\">Use<\/span> <span class=\"hljs-title\">of<\/span> <span class=\"hljs-title\">undefined<\/span> <span class=\"hljs-title\">constant<\/span> <span class=\"hljs-title\">STDIN<\/span> - <span class=\"hljs-title\">assumed<\/span> '<span class=\"hljs-title\">STDIN<\/span>'<\/code><\/p><p class=\"lang-php s-code-block\"><b>Warning<\/b>: Use of undefined constant STDIN &#8211; assumed &#8216;STDIN&#8217; (this will throw an Error in a future version of PHP) in\u00a0<br \/><br \/><b>Warning<\/b>: fgets() expects parameter 1 to be resource, string given in\u00a0on line\u00a0<b>3<\/b><br \/><br \/>Further research shows &#8220;only the\u00a0<strong>CLI<\/strong>\u00a0(command line) SAPI defines\u00a0<a href=\"https:\/\/www.php.net\/manual\/en\/features.commandline.io-streams.php\" rel=\"nofollow noreferrer\">I\/O constants<\/a>\u00a0such as\u00a0<code>STDIN<\/code>,\u00a0<code>STDOUT<\/code>, and\u00a0<code>STDERR<\/code><\/p><p>I didn&#8217;t have access to PHP Cli Easily , so I performed the below ( <a href=\"https:\/\/gist.github.com\/LindaLawton\/cff75182aac5fa42930a09f58b63a309\">this was a big help but it pulled an Application Access Token instead of Web<\/a> )\u00a0<\/p><p>To get this you have to post stuff to google api and get a file<\/p><ol><li>Open Up your Credential.json file and get your Client_id and Client Secret<\/li><li>Change the below to have your Client ID and correct redirect_uri and <a href=\"https:\/\/developers.google.com\/drive\/api\/v3\/about-auth\">scope\u00a0<\/a><br \/>https:\/\/accounts.google.com\/o\/oauth2\/auth?client_id=XXXXXXXXXXX&amp;redirect_uri=XXXXXXXXX&amp;scope=https:\/\/www.googleapis.com\/auth\/drive.metadata.readonly&amp;response_type=code&amp;access_type=offline&amp;prompt=consent<\/li><li>Post this into a browser that&#8217;s logged into the google account where you API is registered Go through the prompts to allow access , after this is done it will take you back to your redirect_uri and give you an Authorisation code per below<\/li><li><img loading=\"lazy\" decoding=\"async\" width=\"1147\" height=\"49\" class=\"alignnone size-full wp-image-5176  img-responsive\" src=\"https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2021\/07\/img_60de4009669e4.png\" alt=\"\" srcset=\"https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2021\/07\/img_60de4009669e4.png 1147w, https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2021\/07\/img_60de4009669e4-300x13.png 300w, https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2021\/07\/img_60de4009669e4-1024x44.png 1024w, https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2021\/07\/img_60de4009669e4-768x33.png 768w\" sizes=\"auto, (max-width: 1147px) 100vw, 1147px\" \/><br \/>Format\u00a0https:\/\/redirect_uri\/?code=[Authorisaton Code]&amp;scope=[SCOPE] ( Copy and save the Authorisation Code )\u00a0<\/li><li>Next you will need a Linux machine with Curl , <a href=\"https:\/\/www.kali.org\/docs\/wsl\/win-kex\/\">you can install Linux on Windows using WSL<\/a><\/li><li>Open up the Linux terminal and paste the below<br \/><br \/>curl \\<\/li><\/ol><p style=\"padding-left: 40px;\">&#8211;request POST \\<br \/>&#8211;data &#8220;code=[Authorisaton Code]&amp;client_id=[CLIENT ID]&amp;client_secret=]CLIENT SECRET]&amp;redirect_uri=[REDIRECT URI]&amp;grant_type=authorization_code&#8221; \\<br \/>https:\/\/accounts.google.com\/o\/oauth2\/token<\/p><p id=\"jUlhPCe\"><img loading=\"lazy\" decoding=\"async\" width=\"1564\" height=\"351\" class=\"alignnone size-full wp-image-5175  img-responsive\" src=\"https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2021\/07\/img_60de3ee59f95e.png\" alt=\"\" srcset=\"https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2021\/07\/img_60de3ee59f95e.png 1564w, https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2021\/07\/img_60de3ee59f95e-300x67.png 300w, https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2021\/07\/img_60de3ee59f95e-1024x230.png 1024w, https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2021\/07\/img_60de3ee59f95e-768x172.png 768w, https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2021\/07\/img_60de3ee59f95e-1536x345.png 1536w\" sizes=\"auto, (max-width: 1564px) 100vw, 1564px\" \/><\/p><p>It will come back with an access_token and refresh_token<\/p><p>create a Json file with this in per below\u00a0<\/p><p>{&#8220;access_token&#8221;:&#8221;xxxxxxxxxxxxxxxxxx&#8221;,&#8221;expires_in&#8221;:3599,&#8221;scope&#8221;:&#8221;https:\\\/\\\/www.googleapis.com\\\/auth\\\/drive.metadata.readonly&#8221;,&#8221;token_type&#8221;:&#8221;Bearer&#8221;,&#8221;created&#8221;:1625147606,&#8221;refresh_token&#8221;:&#8221;xxxxxxxxxxxxxxxxxxxxxx&#8221;}<\/p><p><a href=\"https:\/\/developers.google.com\/drive\/api\/v3\/reference\/permissions\/create?apix_params=%7B%22fileId%22%3A%221Y0O4m_lLdf0iJY4esbsq_LEZnc-hujFM%22%2C%22resource%22%3A%7B%22role%22%3A%22reader%22%2C%22type%22%3A%22user%22%7D%7D\">How to Create Permissions so files are public<\/a><\/p><p>Folder or File ID in fileId<\/p><p>Request Body<\/p><p>{<br \/>&#8220;role&#8221;: &#8220;reader&#8221;,<br \/>&#8220;type&#8221;: &#8220;user&#8221;<br \/>}<\/p>","protected":false},"excerpt":{"rendered":"<p>Recently had to reverse engineer a Google Drive PHP API Script, and I thought I would document the process!Before you go down the below path , you [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[3518,3515,1590,3520,3519,3516,3523,698,3521,3517,3522],"class_list":["post-5174","post","type-post","status-publish","format-standard","hentry","category-research","tag-access-token","tag-access_token","tag-api","tag-credentials-json","tag-curl","tag-google-drive","tag-google-drive-api-access-token-without-cli","tag-php","tag-stdin","tag-token-json","tag-use-of-undefined-constant-stdin"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts\/5174","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/comments?post=5174"}],"version-history":[{"count":7,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts\/5174\/revisions"}],"predecessor-version":[{"id":5227,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts\/5174\/revisions\/5227"}],"wp:attachment":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/media?parent=5174"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/categories?post=5174"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/tags?post=5174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}