{"id":5223,"date":"2021-07-20T06:46:15","date_gmt":"2021-07-20T06:46:15","guid":{"rendered":"https:\/\/pariswells.com\/blog\/?p=5223"},"modified":"2021-07-20T06:46:15","modified_gmt":"2021-07-20T06:46:15","slug":"how-to-use-a-service-account-to-access-google-drive-api-instead-of","status":"publish","type":"post","link":"https:\/\/pariswells.com\/blog\/research\/how-to-use-a-service-account-to-access-google-drive-api-instead-of","title":{"rendered":"How to use a Service account to access Google Drive API instead of"},"content":{"rendered":"<p><a href=\"https:\/\/pariswells.com\/blog\/research\/google-drive-api-credential-json-and-token-json-access-token\">Per Previous post<\/a>\u00a0 , I had to reserve engineer a method for accessing documents in Google drive via API from WordPress , I put the access to Offline so it wouldn&#8217;t need refreshing , however after a week it suddenly stopped working with\u00a0<\/p><p><strong>invalid_grant<\/strong><\/p><p>I put the API in <a href=\"https:\/\/support.google.com\/cloud\/answer\/10311615#publishing-status&amp;zippy=%2Ctesting%2Cin-production%2Cinternal%2Cexternal\">testing<\/a> mode which means\u00a0<\/p><pre>Authorizations by a test user will expire seven days from the time of consent. If your OAuth client requests\u00a0<a href=\"https:\/\/developers.google.com\/identity\/protocols\/oauth2\/web-server#offline\">an\u00a0<code>offline<\/code>\u00a0access type<\/a>\u00a0and receives a refresh token, that token will also expire.<\/pre><p>Changing this to &#8220;<a href=\"https:\/\/support.google.com\/cloud\/answer\/10311615#publishing-status&amp;zippy=%2Ctesting%2Cinternal%2Cexternal\">In production<\/a>&#8221; looks like a lot of trouble\u00a0<\/p><p>However reading through the guides a Service account would fix this task<\/p><p>&nbsp;<\/p><p>Create a service account and create a key for that service account<\/p><p id=\"mmbfats\"><img loading=\"lazy\" decoding=\"async\" width=\"381\" height=\"518\" class=\"alignnone size-full wp-image-5224  img-responsive\" src=\"https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2021\/07\/img_60f670c6d5523.png\" alt=\"\" srcset=\"https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2021\/07\/img_60f670c6d5523.png 381w, https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2021\/07\/img_60f670c6d5523-221x300.png 221w\" sizes=\"auto, (max-width: 381px) 100vw, 381px\" \/><\/p><p id=\"SfkuSBR\"><img loading=\"lazy\" decoding=\"async\" width=\"1106\" height=\"478\" class=\"alignnone size-full wp-image-5225  img-responsive\" src=\"https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2021\/07\/img_60f670d3d6726.png\" alt=\"\" srcset=\"https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2021\/07\/img_60f670d3d6726.png 1106w, https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2021\/07\/img_60f670d3d6726-300x130.png 300w, https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2021\/07\/img_60f670d3d6726-1024x443.png 1024w, https:\/\/pariswells.com\/blog\/wp-content\/uploads\/2021\/07\/img_60f670d3d6726-768x332.png 768w\" sizes=\"auto, (max-width: 1106px) 100vw, 1106px\" \/><\/p><p>JSON key file should look like below<\/p><pre>{<br \/>\"type\": \"service_account\",<br \/>\"project_id\": \"xxxxxxxx\",<br \/>\"private_key_id\": \"xxxxxxxxx\",<br \/>\"private_key\": \"END PRIVATE KEY-----\\n\",<br \/>\"client_email\": \"xxxxx@xxxxxxxx.iam.gserviceaccount.com\",<br \/>\"client_id\": \"xxxxxxxxxxx\",<br \/>\"auth_uri\": \"https:\/\/accounts.google.com\/o\/oauth2\/auth\",<br \/>\"token_uri\": \"https:\/\/oauth2.googleapis.com\/token\",<br \/>\"auth_provider_x509_cert_url\": \"https:\/\/www.googleapis.com\/oauth2\/v1\/certs\",<br \/>\"client_x509_cert_url\": \"https:\/\/www.googleapis.com\/robot\/v1\/metadata\/x509\/xxxxxxxxx.iam.gserviceaccount.com\"<br \/>}<\/pre><p>Save this json as something like service.json<\/p><p><strong>Share the folders in Google Drive with your service account xxxxx@xxxxxxxx.iam.gserviceaccount.com with the right permissions\u00a0<\/strong><\/p><p>Remove the need for token.json<\/p><p><br \/>$client-&gt;setAuthConfig(__DIR__.&#8217;\/service.json&#8217;);<\/p>","protected":false},"excerpt":{"rendered":"<p>Per Previous post\u00a0 , I had to reserve engineer a method for accessing documents in Google drive via API from WordPress , I put the access to [&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":[1590,3555,1601,3553,3554,3517],"class_list":["post-5223","post","type-post","status-publish","format-standard","hentry","category-research","tag-api","tag-drive","tag-google","tag-invalid_grant","tag-service-account","tag-token-json"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts\/5223","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=5223"}],"version-history":[{"count":1,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts\/5223\/revisions"}],"predecessor-version":[{"id":5226,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts\/5223\/revisions\/5226"}],"wp:attachment":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/media?parent=5223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/categories?post=5223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/tags?post=5223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}