5
$\begingroup$

I've followed Google's instructions for generating a local client_secret.json file for programmatic access to my Google Docs, but it's not clear to me how to use this in Mathematica.

There are many related answers here already, but most seem outdated and none mention how to use client_secret.json.

How do I use my Google API client_secret.json to access my Google Docs? For example, how would I import the contents of a Google Sheet?

$\endgroup$
2
  • $\begingroup$ client_secrets.json just stores all the info you’ll then pass to the OAuth connection schemes described elsewhere on the site. You can always import it and use the data in one of the other answers. $\endgroup$
    – b3m2a1
    Commented Nov 14, 2017 at 16:50
  • $\begingroup$ @b3m2a1: Well, yes. The question is how to do that (and how to do it in the current, updated, idiom). $\endgroup$
    – orome
    Commented Nov 14, 2017 at 17:00

1 Answer 1

5
$\begingroup$

Here's how I decided to include this in my service connections.

First, I tweaked my template to search out one of these files in my paclet or the "ApplicationData" subdir of $UserBaseDirectory:

$$serviceconnectionclientsecretsjsonfile=
    If[StringQ@#,
        FileNameJoin@{
            #,
            "client_secrets.json"
            },
        None
        ]&@
    SelectFirst[
        {
            ParentDirectory@DirectoryName@$InputFileName,
            FileNameJoin@{
                $UserBaseDirectory, 
                "ApplicationData",
                "ServiceConnections",
                "$ServiceConnection"
                }
            },
        FileExistsQ@
        FileNameJoin@{
            #,
            "client_secrets.json"
            }&
        ]

Then I download my client_secrets.json and save it to "$UserBaseDirectory/ApplicationData/ServiceConnections/GoogleDrive/client_secrets.json"

Finally I take my standard service connection template and pass this as the client config:

$clientInfo["OAuth"] =
  {
   "Client" -> "OAuth",
   "ClientID" :> googledrivegetclientdata["ClientID"],
   "ClientSecret" :> googledrivegetclientdata["ClientSecret"],
   "AuthorizeEndpoint" :>
    googledrivegetclientdata[
     "AuthURI",
     "https://accounts.google.com/o/oauth2/v2/auth"
     ],
   "AccessEndpoint" :>
    googledrivegetclientdata[
     "TokenURI",
     "https://www.googleapis.com/oauth2/v4/token"
     ],
   "RedirectURI" :>
    googledrivegetclientdata[
     "RedirectURIs",
     {Key["access_code"]},
     First
     ],
   "AuthorizationScope" :>
    googledrivegetclientdata[
     "AuthScope",
     "https://www.googleapis.com/auth/drive"
     ],
   "AuthorizationResponseType" -> "code",
   "Fetch" -> GDRawImport,
   "Information" -> $serviceDescription
   };

The only interesting thing there is the googledrivegetclientdata calls. Basically at load time I import from "client_secrets.json" and then pass those to Mathematica's OAuth flow.

This then works fine. Obviously I can't provide my "client_secrets.json" in my paclet, but you can install the paclet like so:

PacletInstall[
 "ServiceConnection_GoogleDrive",
 "Site"->"http://www.wolframcloud.com/objects/b3m2a1.paclets/PacletServer"
 ]

And put your own "client_secrets.json" at ""$UserBaseDirectory/ApplicationData/ServiceConnections/GoogleDrive/client_secrets.json"

And assuming I didn't mess anything up it should work on your machine too.

That paclet itself is a little glitchy and I don't have time now to expand or patch it, but if you look at its innards it should be clear enough what's happening.

$\endgroup$
2
  • $\begingroup$ How about without a paclet. I don't use packages or paclets. $\endgroup$
    – orome
    Commented Nov 15, 2017 at 13:26
  • $\begingroup$ @orome time consuming and not worth it for me. In that case just do what was suggested earlier: take C.E.’s answer previously and just import the file into the request parameters. $\endgroup$
    – b3m2a1
    Commented Nov 15, 2017 at 15:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.