xref: /petsc/src/sys/webclient/google.c (revision c6a7a37075f8bf8d34d92c4910d42445b7a3482d)
10efc6a03SBarry Smith 
20efc6a03SBarry Smith #include <petscwebclient.h>
3bb04b57dSBarry Smith #pragma clang diagnostic ignored "-Wdeprecated-declarations"
445e40e47SBarry Smith #pragma gcc diagnostic   ignored "-Wdeprecated-declarations"
50efc6a03SBarry Smith 
60efc6a03SBarry Smith /*
70efc6a03SBarry Smith    These variables identify the code as a PETSc application to Google.
80efc6a03SBarry Smith 
9a8d69d7bSBarry Smith    See -   https://stackoverflow.com/questions/4616553/using-oauth-in-free-open-source-software
100efc6a03SBarry Smith    Users can get their own application IDs - https://code.google.com/p/google-apps-manager/wiki/GettingAnOAuthConsoleKey
110efc6a03SBarry Smith 
120efc6a03SBarry Smith */
130efc6a03SBarry Smith #define PETSC_GOOGLE_CLIENT_ID "521429262559-i19i57eek8tnt9ftpp4p91rcl0bo9ag5.apps.googleusercontent.com"
140efc6a03SBarry Smith #define PETSC_GOOGLE_CLIENT_ST "vOds_A71I3_S_aHMq_kZAI0t"
155708bc22SBarry Smith #define PETSC_GOOGLE_API_KEY   "AIzaSyDRZsOcySpWVzsUvIBL2UG3J2tcg-MXbyk"
160efc6a03SBarry Smith 
170efc6a03SBarry Smith /*@C
180efc6a03SBarry Smith      PetscGoogleDriveRefresh - Get a new authorization token for accessing Google drive from PETSc from a refresh token
190efc6a03SBarry Smith 
20811af0c4SBarry Smith    Not collective, only the first process in the `MPI_Comm` does anything
210efc6a03SBarry Smith 
220efc6a03SBarry Smith    Input Parameters:
230efc6a03SBarry Smith +   comm - MPI communicator
24811af0c4SBarry Smith .   refresh token - obtained with `PetscGoogleDriveAuthorize()`, if NULL PETSc will first look for one in the options data
25811af0c4SBarry Smith                     if not found it will call `PetscGoogleDriveAuthorize()`
260efc6a03SBarry Smith -   tokensize - size of the output string access_token
270efc6a03SBarry Smith 
280efc6a03SBarry Smith    Output Parameter:
29811af0c4SBarry Smith .   access_token - token that can be passed to `PetscGoogleDriveUpload()`
300efc6a03SBarry Smith 
31811af0c4SBarry Smith    Options Database Key:
32811af0c4SBarry Smith .  -google_refresh_token XXX - where XXX was obtained from `PetscGoogleDriveAuthorize()`
334a285bdaSBarry Smith 
342b26979fSBarry Smith    Level: intermediate
354a285bdaSBarry Smith 
36db781477SPatrick Sanan .seealso: `PetscURLShorten()`, `PetscGoogleDriveAuthorize()`, `PetscGoogleDriveUpload()`
370efc6a03SBarry Smith @*/
38d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscGoogleDriveRefresh(MPI_Comm comm, const char refresh_token[], char access_token[], size_t tokensize)
39d71ae5a4SJacob Faibussowitsch {
400efc6a03SBarry Smith   SSL_CTX    *ctx;
410efc6a03SBarry Smith   SSL        *ssl;
420efc6a03SBarry Smith   int         sock;
435dc0f0a4SBarry Smith   char        buff[8 * 1024], body[1024];
440efc6a03SBarry Smith   PetscMPIInt rank;
450efc6a03SBarry Smith   char       *refreshtoken = (char *)refresh_token;
465dc0f0a4SBarry Smith   PetscBool   found;
470efc6a03SBarry Smith 
480efc6a03SBarry Smith   PetscFunctionBegin;
499566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
50dd400576SPatrick Sanan   if (rank == 0) {
510efc6a03SBarry Smith     if (!refresh_token) {
520efc6a03SBarry Smith       PetscBool set;
539566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(512, &refreshtoken));
549566063dSJacob Faibussowitsch       PetscCall(PetscOptionsGetString(NULL, NULL, "-google_refresh_token", refreshtoken, sizeof(refreshtoken), &set));
550efc6a03SBarry Smith       if (!set) {
569566063dSJacob Faibussowitsch         PetscCall(PetscGoogleDriveAuthorize(comm, access_token, refreshtoken, 512 * sizeof(char)));
579566063dSJacob Faibussowitsch         PetscCall(PetscFree(refreshtoken));
583ba16761SJacob Faibussowitsch         PetscFunctionReturn(PETSC_SUCCESS);
590efc6a03SBarry Smith       }
600efc6a03SBarry Smith     }
619566063dSJacob Faibussowitsch     PetscCall(PetscSSLInitializeContext(&ctx));
629566063dSJacob Faibussowitsch     PetscCall(PetscHTTPSConnect("accounts.google.com", 443, ctx, &sock, &ssl));
63*c6a7a370SJeremy L Thompson     PetscCall(PetscStrncpy(body, "client_id=", sizeof(body)));
64*c6a7a370SJeremy L Thompson     PetscCall(PetscStrlcat(body, PETSC_GOOGLE_CLIENT_ID, sizeof(body)));
65*c6a7a370SJeremy L Thompson     PetscCall(PetscStrlcat(body, "&client_secret=", sizeof(body)));
66*c6a7a370SJeremy L Thompson     PetscCall(PetscStrlcat(body, PETSC_GOOGLE_CLIENT_ST, sizeof(body)));
67*c6a7a370SJeremy L Thompson     PetscCall(PetscStrlcat(body, "&refresh_token=", sizeof(body)));
68*c6a7a370SJeremy L Thompson     PetscCall(PetscStrlcat(body, refreshtoken, sizeof(body)));
699566063dSJacob Faibussowitsch     if (!refresh_token) PetscCall(PetscFree(refreshtoken));
70*c6a7a370SJeremy L Thompson     PetscCall(PetscStrlcat(body, "&grant_type=refresh_token", sizeof(body)));
710efc6a03SBarry Smith 
729566063dSJacob Faibussowitsch     PetscCall(PetscHTTPSRequest("POST", "accounts.google.com/o/oauth2/token", NULL, "application/x-www-form-urlencoded", body, ssl, buff, sizeof(buff)));
739566063dSJacob Faibussowitsch     PetscCall(PetscSSLDestroyContext(ctx));
740efc6a03SBarry Smith     close(sock);
750efc6a03SBarry Smith 
769566063dSJacob Faibussowitsch     PetscCall(PetscPullJSONValue(buff, "access_token", access_token, tokensize, &found));
7728b400f6SJacob Faibussowitsch     PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_LIB, "Google drive did not return access_token");
780efc6a03SBarry Smith   }
793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
800efc6a03SBarry Smith }
810efc6a03SBarry Smith 
820efc6a03SBarry Smith #include <sys/stat.h>
830efc6a03SBarry Smith 
840efc6a03SBarry Smith /*@C
850efc6a03SBarry Smith      PetscGoogleDriveUpload - Loads a file to the Google Drive
860efc6a03SBarry Smith 
87811af0c4SBarry Smith      Not collective, only the first process in the `MPI_Comm` uploads the file
880efc6a03SBarry Smith 
890efc6a03SBarry Smith   Input Parameters:
900efc6a03SBarry Smith +   comm - MPI communicator
910efc6a03SBarry Smith .   access_token - obtained with PetscGoogleDriveRefresh(), pass NULL to have PETSc generate one
920efc6a03SBarry Smith -   filename - file to upload; if you upload multiple times it will have different names each time on Google Drive
930efc6a03SBarry Smith 
94811af0c4SBarry Smith   Options Database Key:
95147403d9SBarry Smith .  -google_refresh_token XXX - pass the access token for the operation
960efc6a03SBarry Smith 
970efc6a03SBarry Smith   Usage Patterns:
98811af0c4SBarry Smith .vb
994a285bdaSBarry Smith     With PETSc option -google_refresh_token  XXX given
1000efc6a03SBarry Smith     PetscGoogleDriveUpload(comm,NULL,filename);        will upload file with no user interaction
1010efc6a03SBarry Smith 
1024a285bdaSBarry Smith     Without PETSc option -google_refresh_token XXX given
1034683183fSBarry Smith     PetscGoogleDriveUpload(comm,NULL,filename);        for first use will prompt user to authorize access to Google Drive with their browser
1040efc6a03SBarry Smith 
1054a285bdaSBarry Smith     With PETSc option -google_refresh_token  XXX given
1060efc6a03SBarry Smith     PetscGoogleDriveRefresh(comm,NULL,access_token,sizeof(access_token));
1070efc6a03SBarry Smith     PetscGoogleDriveUpload(comm,access_token,filename);
1080efc6a03SBarry Smith 
1090efc6a03SBarry Smith     With refresh token entered in some way by the user
1100efc6a03SBarry Smith     PetscGoogleDriveRefresh(comm,refresh_token,access_token,sizeof(access_token));
1110efc6a03SBarry Smith     PetscGoogleDriveUpload(comm,access_token,filename);
1120efc6a03SBarry Smith 
1130efc6a03SBarry Smith     PetscGoogleDriveAuthorize(comm,access_token,refresh_token,sizeof(access_token));
1140efc6a03SBarry Smith     PetscGoogleDriveUpload(comm,access_token,filename);
115811af0c4SBarry Smith .ve
1160efc6a03SBarry Smith 
1172b26979fSBarry Smith    Level: intermediate
1182b26979fSBarry Smith 
119db781477SPatrick Sanan .seealso: `PetscURLShorten()`, `PetscGoogleDriveAuthorize()`, `PetscGoogleDriveRefresh()`
1200efc6a03SBarry Smith @*/
121d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscGoogleDriveUpload(MPI_Comm comm, const char access_token[], const char filename[])
122d71ae5a4SJacob Faibussowitsch {
1230efc6a03SBarry Smith   SSL_CTX    *ctx;
1240efc6a03SBarry Smith   SSL        *ssl;
1250efc6a03SBarry Smith   int         sock;
1260efc6a03SBarry Smith   char        head[1024], buff[8 * 1024], *body, *title;
1270efc6a03SBarry Smith   PetscMPIInt rank;
1280efc6a03SBarry Smith   struct stat sb;
1290efc6a03SBarry Smith   size_t      len, blen, rd;
1300efc6a03SBarry Smith   FILE       *fd;
1312da392ccSBarry Smith   int         err;
1320efc6a03SBarry Smith 
1330efc6a03SBarry Smith   PetscFunctionBegin;
1349566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
135dd400576SPatrick Sanan   if (rank == 0) {
136*c6a7a370SJeremy L Thompson     PetscCall(PetscStrncpy(head, "Authorization: Bearer ", sizeof(head)));
137*c6a7a370SJeremy L Thompson     PetscCall(PetscStrlcat(head, access_token, sizeof(head)));
138*c6a7a370SJeremy L Thompson     PetscCall(PetscStrlcat(head, "\r\n", sizeof(head)));
139*c6a7a370SJeremy L Thompson     PetscCall(PetscStrlcat(head, "uploadType: multipart\r\n", sizeof(head)));
1400efc6a03SBarry Smith 
1412da392ccSBarry Smith     err = stat(filename, &sb);
14228b400f6SJacob Faibussowitsch     PetscCheck(!err, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "Unable to stat file: %s", filename);
1430efc6a03SBarry Smith     len = 1024 + sb.st_size;
1449566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(len, &body));
145*c6a7a370SJeremy L Thompson     PetscCall(PetscStrncpy(body,
1460efc6a03SBarry Smith                            "--foo_bar_baz\r\n"
147*c6a7a370SJeremy L Thompson                            "Content-Type: application/json\r\n\r\n"
148*c6a7a370SJeremy L Thompson                            "{",
149*c6a7a370SJeremy L Thompson                            sizeof(body)));
150*c6a7a370SJeremy L Thompson     PetscCall(PetscPushJSONValue(body, "title", filename, len));
151*c6a7a370SJeremy L Thompson     PetscCall(PetscStrlcat(body, ",", sizeof(body)));
152*c6a7a370SJeremy L Thompson     PetscCall(PetscPushJSONValue(body, "mimeType", "text.html", len));
153*c6a7a370SJeremy L Thompson     PetscCall(PetscStrlcat(body, ",", sizeof(body)));
154*c6a7a370SJeremy L Thompson     PetscCall(PetscPushJSONValue(body, "description", "a file", len));
155*c6a7a370SJeremy L Thompson     PetscCall(PetscStrlcat(body,
156*c6a7a370SJeremy L Thompson                            "}\r\n\r\n"
157*c6a7a370SJeremy L Thompson                            "--foo_bar_baz\r\n"
158*c6a7a370SJeremy L Thompson                            "Content-Type: text/html\r\n\r\n",
159*c6a7a370SJeremy L Thompson                            sizeof(body)));
1609566063dSJacob Faibussowitsch     PetscCall(PetscStrlen(body, &blen));
1610efc6a03SBarry Smith     fd = fopen(filename, "r");
16228b400f6SJacob Faibussowitsch     PetscCheck(fd, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "Unable to open file: %s", filename);
1630efc6a03SBarry Smith     rd = fread(body + blen, sizeof(unsigned char), sb.st_size, fd);
164d8174014SToby Isaac     PetscCheck(rd == (size_t)sb.st_size, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "Unable to read entire file: %s %d %d", filename, (int)rd, (int)sb.st_size);
1650efc6a03SBarry Smith     fclose(fd);
1660efc6a03SBarry Smith     body[blen + rd] = 0;
167*c6a7a370SJeremy L Thompson     PetscCall(PetscStrlcat(body,
168*c6a7a370SJeremy L Thompson                            "\r\n\r\n"
169*c6a7a370SJeremy L Thompson                            "--foo_bar_baz\r\n",
170*c6a7a370SJeremy L Thompson                            sizeof(body)));
1719566063dSJacob Faibussowitsch     PetscCall(PetscSSLInitializeContext(&ctx));
1729566063dSJacob Faibussowitsch     PetscCall(PetscHTTPSConnect("www.googleapis.com", 443, ctx, &sock, &ssl));
1739566063dSJacob Faibussowitsch     PetscCall(PetscHTTPSRequest("POST", "www.googleapis.com/upload/drive/v2/files/", head, "multipart/related; boundary=\"foo_bar_baz\"", body, ssl, buff, sizeof(buff)));
1749566063dSJacob Faibussowitsch     PetscCall(PetscFree(body));
1759566063dSJacob Faibussowitsch     PetscCall(PetscSSLDestroyContext(ctx));
1760efc6a03SBarry Smith     close(sock);
1779566063dSJacob Faibussowitsch     PetscCall(PetscStrstr(buff, "\"title\"", &title));
17828b400f6SJacob Faibussowitsch     PetscCheck(title, PETSC_COMM_SELF, PETSC_ERR_LIB, "Upload of file %s failed", filename);
1790efc6a03SBarry Smith   }
1803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1810efc6a03SBarry Smith }
1820efc6a03SBarry Smith 
18368e69593SBarry Smith #if defined(PETSC_HAVE_UNISTD_H)
18468e69593SBarry Smith   #include <unistd.h>
18568e69593SBarry Smith #endif
18668e69593SBarry Smith 
1870efc6a03SBarry Smith /*@C
1880efc6a03SBarry Smith      PetscGoogleDriveAuthorize - Get authorization and refresh token for accessing Google drive from PETSc
1890efc6a03SBarry Smith 
190811af0c4SBarry Smith    Not collective, only the first process in `MPI_Comm` does anything
1910efc6a03SBarry Smith 
1920efc6a03SBarry Smith    Input Parameters:
1930efc6a03SBarry Smith +  comm - the MPI communicator
1940efc6a03SBarry Smith -  tokensize - size of the token arrays
1950efc6a03SBarry Smith 
1960efc6a03SBarry Smith    Output Parameters:
197811af0c4SBarry Smith +  access_token - can be used with `PetscGoogleDriveUpload()` for this one session
198811af0c4SBarry Smith -  refresh_token - can be used for ever to obtain new access_tokens with `PetscGoogleDriveRefresh()`, guard this like a password
1990efc6a03SBarry Smith                    it gives access to your Google Drive
2000efc6a03SBarry Smith 
20195452b02SPatrick Sanan    Notes:
20295452b02SPatrick Sanan     This call requires stdout and stdin access from process 0 on the MPI communicator
2030efc6a03SBarry Smith 
204c4762a1bSJed Brown    You can run src/sys/webclient/tutorials/googleobtainrefreshtoken to get a refresh token and then in the future pass it to
2054a285bdaSBarry Smith    PETSc programs with -google_refresh_token XXX
2060efc6a03SBarry Smith 
2072b26979fSBarry Smith    Level: intermediate
2082b26979fSBarry Smith 
209db781477SPatrick Sanan .seealso: `PetscGoogleDriveRefresh()`, `PetscGoogleDriveUpload()`, `PetscURLShorten()`
2100efc6a03SBarry Smith @*/
211d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscGoogleDriveAuthorize(MPI_Comm comm, char access_token[], char refresh_token[], size_t tokensize)
212d71ae5a4SJacob Faibussowitsch {
2130efc6a03SBarry Smith   SSL_CTX    *ctx;
2140efc6a03SBarry Smith   SSL        *ssl;
2150efc6a03SBarry Smith   int         sock;
2165dc0f0a4SBarry Smith   char        buff[8 * 1024], *ptr, body[1024];
2170efc6a03SBarry Smith   PetscMPIInt rank;
2180efc6a03SBarry Smith   size_t      len;
2195dc0f0a4SBarry Smith   PetscBool   found;
2200efc6a03SBarry Smith 
2210efc6a03SBarry Smith   PetscFunctionBegin;
2229566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
223dd400576SPatrick Sanan   if (rank == 0) {
224cc73adaaSBarry Smith     PetscCheck(isatty(fileno(PETSC_STDOUT)), PETSC_COMM_SELF, PETSC_ERR_USER, "Requires users input/output");
225d0609cedSBarry Smith     PetscCall(PetscPrintf(comm, "Cut and paste the following into your browser:\n\n"
2260efc6a03SBarry Smith                                 "https://accounts.google.com/o/oauth2/auth?"
2270efc6a03SBarry Smith                                 "scope=https%%3A%%2F%%2Fwww.googleapis.com%%2Fauth%%2Fdrive.file&"
2280efc6a03SBarry Smith                                 "redirect_uri=urn:ietf:wg:oauth:2.0:oob&"
2290efc6a03SBarry Smith                                 "response_type=code&"
2309371c9d4SSatish Balay                                 "client_id=" PETSC_GOOGLE_CLIENT_ID "\n\n"));
2319566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(comm, "Paste the result here:"));
2320efc6a03SBarry Smith     ptr = fgets(buff, 1024, stdin);
23328b400f6SJacob Faibussowitsch     PetscCheck(ptr, PETSC_COMM_SELF, PETSC_ERR_FILE_READ, "Error reading from stdin: %d", errno);
2349566063dSJacob Faibussowitsch     PetscCall(PetscStrlen(buff, &len));
2350efc6a03SBarry Smith     buff[len - 1] = 0; /* remove carriage return at end of line */
2360efc6a03SBarry Smith 
2379566063dSJacob Faibussowitsch     PetscCall(PetscSSLInitializeContext(&ctx));
2389566063dSJacob Faibussowitsch     PetscCall(PetscHTTPSConnect("accounts.google.com", 443, ctx, &sock, &ssl));
239*c6a7a370SJeremy L Thompson     PetscCall(PetscStrncpy(body, "code=", sizeof(body)));
240*c6a7a370SJeremy L Thompson     PetscCall(PetscStrlcat(body, buff, sizeof(body)));
241*c6a7a370SJeremy L Thompson     PetscCall(PetscStrlcat(body, "&client_id=", sizeof(body)));
242*c6a7a370SJeremy L Thompson     PetscCall(PetscStrlcat(body, PETSC_GOOGLE_CLIENT_ID, sizeof(body)));
243*c6a7a370SJeremy L Thompson     PetscCall(PetscStrlcat(body, "&client_secret=", sizeof(body)));
244*c6a7a370SJeremy L Thompson     PetscCall(PetscStrlcat(body, PETSC_GOOGLE_CLIENT_ST, sizeof(body)));
245*c6a7a370SJeremy L Thompson     PetscCall(PetscStrlcat(body, "&redirect_uri=urn:ietf:wg:oauth:2.0:oob&", sizeof(body)));
246*c6a7a370SJeremy L Thompson     PetscCall(PetscStrlcat(body, "grant_type=authorization_code", sizeof(body)));
2470efc6a03SBarry Smith 
2489566063dSJacob Faibussowitsch     PetscCall(PetscHTTPSRequest("POST", "accounts.google.com/o/oauth2/token", NULL, "application/x-www-form-urlencoded", body, ssl, buff, sizeof(buff)));
2499566063dSJacob Faibussowitsch     PetscCall(PetscSSLDestroyContext(ctx));
2500efc6a03SBarry Smith     close(sock);
2510efc6a03SBarry Smith 
2529566063dSJacob Faibussowitsch     PetscCall(PetscPullJSONValue(buff, "access_token", access_token, tokensize, &found));
25328b400f6SJacob Faibussowitsch     PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_LIB, "Google drive did not return access_token");
2549566063dSJacob Faibussowitsch     PetscCall(PetscPullJSONValue(buff, "refresh_token", refresh_token, tokensize, &found));
25528b400f6SJacob Faibussowitsch     PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_LIB, "Google drive did not return refresh_token");
2560efc6a03SBarry Smith 
2579566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(comm, "Here is your Google refresh token, save it in a save place, in the future you can run PETSc\n"));
2589566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(comm, "programs with the option -google_refresh_token %s\n", refresh_token));
2599566063dSJacob Faibussowitsch     PetscCall(PetscPrintf(comm, "to access Google Drive automatically\n"));
2600efc6a03SBarry Smith   }
2613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2620efc6a03SBarry Smith }
2630efc6a03SBarry Smith 
2640efc6a03SBarry Smith /*@C
2650efc6a03SBarry Smith      PetscURLShorten - Uses Google's service to get a short url for a long url
2660efc6a03SBarry Smith 
2670efc6a03SBarry Smith     Input Parameters:
2680efc6a03SBarry Smith +    url - long URL you want shortened
2690efc6a03SBarry Smith -    lenshorturl - length of buffer to contain short URL
2700efc6a03SBarry Smith 
2710efc6a03SBarry Smith     Output Parameter:
2720efc6a03SBarry Smith .    shorturl - the shortened URL
2730efc6a03SBarry Smith 
2742b26979fSBarry Smith    Level: intermediate
2752b26979fSBarry Smith 
276811af0c4SBarry Smith    Note:
277811af0c4SBarry Smith    Google no longer provides this service so this routine will no longer function
278811af0c4SBarry Smith 
279db781477SPatrick Sanan .seealso: `PetscGoogleDriveRefresh()`, `PetscGoogleDriveUpload()`, `PetscGoogleDriveAuthorize()`
2800efc6a03SBarry Smith @*/
281d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscURLShorten(const char url[], char shorturl[], size_t lenshorturl)
282d71ae5a4SJacob Faibussowitsch {
2830efc6a03SBarry Smith   SSL_CTX  *ctx;
2840efc6a03SBarry Smith   SSL      *ssl;
2850efc6a03SBarry Smith   int       sock;
2865708bc22SBarry Smith   char      buff[1024], body[512], post[1024];
2875dc0f0a4SBarry Smith   PetscBool found;
2880efc6a03SBarry Smith 
2890efc6a03SBarry Smith   PetscFunctionBegin;
2909566063dSJacob Faibussowitsch   PetscCall(PetscSSLInitializeContext(&ctx));
2919566063dSJacob Faibussowitsch   PetscCall(PetscHTTPSConnect("www.googleapis.com", 443, ctx, &sock, &ssl));
292*c6a7a370SJeremy L Thompson   PetscCall(PetscStrncpy(body, "{", sizeof(body)));
2939566063dSJacob Faibussowitsch   PetscCall(PetscPushJSONValue(body, "longUrl", url, sizeof(body) - 2));
294*c6a7a370SJeremy L Thompson   PetscCall(PetscStrlcat(body, "}", sizeof(body)));
2959566063dSJacob Faibussowitsch   PetscCall(PetscSNPrintf(post, sizeof(post), "www.googleapis.com/urlshortener/v1/url?key=%s", PETSC_GOOGLE_API_KEY));
2969566063dSJacob Faibussowitsch   PetscCall(PetscHTTPSRequest("POST", post, NULL, "application/json", body, ssl, buff, sizeof(buff)));
2979566063dSJacob Faibussowitsch   PetscCall(PetscSSLDestroyContext(ctx));
2980efc6a03SBarry Smith   close(sock);
2995dc0f0a4SBarry Smith 
3009566063dSJacob Faibussowitsch   PetscCall(PetscPullJSONValue(buff, "id", shorturl, lenshorturl, &found));
30128b400f6SJacob Faibussowitsch   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_LIB, "Google drive did not return short URL");
3023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3030efc6a03SBarry Smith }
304