xref: /petsc/src/sys/webclient/google.c (revision 5dc0f0a4f1e5fadabb4a56402c5d08263bae98d2)
10efc6a03SBarry Smith 
20efc6a03SBarry Smith #include <petscwebclient.h>
30efc6a03SBarry Smith 
40efc6a03SBarry Smith /*
50efc6a03SBarry Smith    These variables identify the code as a PETSc application to Google.
60efc6a03SBarry Smith 
70efc6a03SBarry Smith    See -   http://stackoverflow.com/questions/4616553/using-oauth-in-free-open-source-software
80efc6a03SBarry Smith    Users can get their own application IDs - https://code.google.com/p/google-apps-manager/wiki/GettingAnOAuthConsoleKey
90efc6a03SBarry Smith 
100efc6a03SBarry Smith */
110efc6a03SBarry Smith #define PETSC_GOOGLE_CLIENT_ID  "521429262559-i19i57eek8tnt9ftpp4p91rcl0bo9ag5.apps.googleusercontent.com"
120efc6a03SBarry Smith #define PETSC_GOOGLE_CLIENT_ST  "vOds_A71I3_S_aHMq_kZAI0t"
130efc6a03SBarry Smith 
140efc6a03SBarry Smith 
150efc6a03SBarry Smith #undef __FUNCT__
160efc6a03SBarry Smith #define __FUNCT__ "PetscGoogleDriveRefresh"
170efc6a03SBarry Smith /*@C
180efc6a03SBarry Smith      PetscGoogleDriveRefresh - Get a new authorization token for accessing Google drive from PETSc from a refresh token
190efc6a03SBarry Smith 
200efc6a03SBarry Smith    Not collective, only the first process in the MPI_Comm does anything
210efc6a03SBarry Smith 
220efc6a03SBarry Smith    Input Parameters:
230efc6a03SBarry Smith +   comm - MPI communicator
240efc6a03SBarry Smith .   refresh token - obtained with PetscGoogleDriveAuthorize(), if NULL PETSc will first look for one in the options data
250efc6a03SBarry Smith                     if not found it will call PetscGoogleDriveAuthorize()
260efc6a03SBarry Smith -   tokensize - size of the output string access_token
270efc6a03SBarry Smith 
280efc6a03SBarry Smith    Output Parameter:
290efc6a03SBarry Smith .   access_token - token that can be passed to PetscGoogleDriveUpload()
300efc6a03SBarry Smith 
314a285bdaSBarry Smith    Options Database:
324a285bdaSBarry Smith .  -google_refresh_token XXX   where XXX was obtained from PetscGoogleDriveAuthorize()
334a285bdaSBarry Smith 
344a285bdaSBarry Smith 
350efc6a03SBarry Smith .seealso: PetscURLShorten(), PetscGoogleDriveAuthorize(), PetscGoogleDriveUpload()
360efc6a03SBarry Smith 
370efc6a03SBarry Smith @*/
380efc6a03SBarry Smith PetscErrorCode PetscGoogleDriveRefresh(MPI_Comm comm,const char refresh_token[],char access_token[],size_t tokensize)
390efc6a03SBarry Smith {
400efc6a03SBarry Smith   SSL_CTX        *ctx;
410efc6a03SBarry Smith   SSL            *ssl;
420efc6a03SBarry Smith   int            sock;
430efc6a03SBarry Smith   PetscErrorCode ierr;
44*5dc0f0a4SBarry Smith   char           buff[8*1024],body[1024];
450efc6a03SBarry Smith   PetscMPIInt    rank;
460efc6a03SBarry Smith   char           *refreshtoken = (char*)refresh_token;
47*5dc0f0a4SBarry Smith   PetscBool      found;
480efc6a03SBarry Smith 
490efc6a03SBarry Smith   PetscFunctionBegin;
500efc6a03SBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
510efc6a03SBarry Smith   if (!rank) {
520efc6a03SBarry Smith     if (!refresh_token) {
530efc6a03SBarry Smith       PetscBool set;
540efc6a03SBarry Smith       ierr = PetscMalloc1(512,&refreshtoken);CHKERRQ(ierr);
554a285bdaSBarry Smith       ierr = PetscOptionsGetString(NULL,"-google_refresh_token",refreshtoken,512,&set);CHKERRQ(ierr);
560efc6a03SBarry Smith       if (!set) {
570efc6a03SBarry Smith         ierr = PetscGoogleDriveAuthorize(comm,access_token,refreshtoken,512*sizeof(char));CHKERRQ(ierr);
580efc6a03SBarry Smith         ierr = PetscFree(refreshtoken);CHKERRQ(ierr);
590efc6a03SBarry Smith         PetscFunctionReturn(0);
600efc6a03SBarry Smith       }
610efc6a03SBarry Smith     }
620efc6a03SBarry Smith     ierr = PetscSSLInitializeContext(&ctx);CHKERRQ(ierr);
630efc6a03SBarry Smith     ierr = PetscHTTPSConnect("accounts.google.com",443,ctx,&sock,&ssl);CHKERRQ(ierr);
644a285bdaSBarry Smith     ierr = PetscStrcpy(body,"client_id=");CHKERRQ(ierr);
650efc6a03SBarry Smith     ierr = PetscStrcat(body,PETSC_GOOGLE_CLIENT_ID);CHKERRQ(ierr);
660efc6a03SBarry Smith     ierr = PetscStrcat(body,"&client_secret=");CHKERRQ(ierr);
670efc6a03SBarry Smith     ierr = PetscStrcat(body,PETSC_GOOGLE_CLIENT_ST);CHKERRQ(ierr);
680efc6a03SBarry Smith     ierr = PetscStrcat(body,"&refresh_token=");CHKERRQ(ierr);
690efc6a03SBarry Smith     ierr = PetscStrcat(body,refreshtoken);CHKERRQ(ierr);
700efc6a03SBarry Smith     if (!refresh_token) {ierr = PetscFree(refreshtoken);CHKERRQ(ierr);}
710efc6a03SBarry Smith     ierr = PetscStrcat(body,"&grant_type=refresh_token");CHKERRQ(ierr);
720efc6a03SBarry Smith 
7393e1d32fSBarry Smith     ierr = PetscHTTPSRequest("POST","accounts.google.com/o/oauth2/token",NULL,"application/x-www-form-urlencoded",body,ssl,buff,sizeof(buff));CHKERRQ(ierr);
740efc6a03SBarry Smith     ierr = PetscSSLDestroyContext(ctx);CHKERRQ(ierr);
750efc6a03SBarry Smith     close(sock);
760efc6a03SBarry Smith 
77*5dc0f0a4SBarry Smith     ierr   = PetscPullJSONValue(buff,"access_token",access_token,tokensize,&found);CHKERRQ(ierr);
78*5dc0f0a4SBarry Smith     if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Google drive did not return access_token");
790efc6a03SBarry Smith   }
800efc6a03SBarry Smith   PetscFunctionReturn(0);
810efc6a03SBarry Smith }
820efc6a03SBarry Smith 
830efc6a03SBarry Smith #include <sys/stat.h>
840efc6a03SBarry Smith 
850efc6a03SBarry Smith #undef __FUNCT__
860efc6a03SBarry Smith #define __FUNCT__ "PetscGoogleDriveUpload"
870efc6a03SBarry Smith /*@C
880efc6a03SBarry Smith      PetscGoogleDriveUpload - Loads a file to the Google Drive
890efc6a03SBarry Smith 
900efc6a03SBarry Smith      Not collective, only the first process in the MPI_Comm uploads the file
910efc6a03SBarry Smith 
920efc6a03SBarry Smith   Input Parameters:
930efc6a03SBarry Smith +   comm - MPI communicator
940efc6a03SBarry Smith .   access_token - obtained with PetscGoogleDriveRefresh(), pass NULL to have PETSc generate one
950efc6a03SBarry Smith -   filename - file to upload; if you upload multiple times it will have different names each time on Google Drive
960efc6a03SBarry Smith 
970efc6a03SBarry Smith   Options Database:
984a285bdaSBarry Smith .  -google_refresh_token   XXX
990efc6a03SBarry Smith 
1000efc6a03SBarry Smith   Usage Patterns:
1014a285bdaSBarry Smith     With PETSc option -google_refresh_token  XXX given
1020efc6a03SBarry Smith     PetscGoogleDriveUpload(comm,NULL,filename);        will upload file with no user interaction
1030efc6a03SBarry Smith 
1044a285bdaSBarry Smith     Without PETSc option -google_refresh_token XXX given
1050efc6a03SBarry Smith     PetscGoogleDriveUpload(comm,NULL,filename);        for first use will prompt user to authorize access to Google Drive with their processor
1060efc6a03SBarry Smith 
1074a285bdaSBarry Smith     With PETSc option -google_refresh_token  XXX given
1080efc6a03SBarry Smith     PetscGoogleDriveRefresh(comm,NULL,access_token,sizeof(access_token));
1090efc6a03SBarry Smith     PetscGoogleDriveUpload(comm,access_token,filename);
1100efc6a03SBarry Smith 
1110efc6a03SBarry Smith     With refresh token entered in some way by the user
1120efc6a03SBarry Smith     PetscGoogleDriveRefresh(comm,refresh_token,access_token,sizeof(access_token));
1130efc6a03SBarry Smith     PetscGoogleDriveUpload(comm,access_token,filename);
1140efc6a03SBarry Smith 
1150efc6a03SBarry Smith     PetscGoogleDriveAuthorize(comm,access_token,refresh_token,sizeof(access_token));
1160efc6a03SBarry Smith     PetscGoogleDriveUpload(comm,access_token,filename);
1170efc6a03SBarry Smith 
1180efc6a03SBarry Smith .seealso: PetscURLShorten(), PetscGoogleDriveAuthorize(), PetscGoogleDriveRefresh()
1190efc6a03SBarry Smith 
1200efc6a03SBarry Smith @*/
1210efc6a03SBarry Smith PetscErrorCode PetscGoogleDriveUpload(MPI_Comm comm,const char access_token[],const char filename[])
1220efc6a03SBarry Smith {
1230efc6a03SBarry Smith   SSL_CTX        *ctx;
1240efc6a03SBarry Smith   SSL            *ssl;
1250efc6a03SBarry Smith   int            sock;
1260efc6a03SBarry Smith   PetscErrorCode ierr;
1270efc6a03SBarry Smith   char           head[1024],buff[8*1024],*body,*title;
1280efc6a03SBarry Smith   PetscMPIInt    rank;
1290efc6a03SBarry Smith   struct stat    sb;
1300efc6a03SBarry Smith   size_t         len,blen,rd;
1310efc6a03SBarry Smith   FILE           *fd;
1320efc6a03SBarry Smith 
1330efc6a03SBarry Smith   PetscFunctionBegin;
1340efc6a03SBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
1350efc6a03SBarry Smith   if (!rank) {
1360efc6a03SBarry Smith     ierr = PetscStrcpy(head,"Authorization: Bearer ");CHKERRQ(ierr);
1370efc6a03SBarry Smith     ierr = PetscStrcat(head,access_token);CHKERRQ(ierr);
1380efc6a03SBarry Smith     ierr = PetscStrcat(head,"\r\n");CHKERRQ(ierr);
1390efc6a03SBarry Smith     ierr = PetscStrcat(head,"uploadType: multipart\r\n");CHKERRQ(ierr);
1400efc6a03SBarry Smith 
1410efc6a03SBarry Smith     ierr = stat(filename,&sb);
1420efc6a03SBarry Smith     if (ierr) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to stat file: %s",filename);
1430efc6a03SBarry Smith     len = 1024 + sb.st_size;
1440efc6a03SBarry Smith     ierr = PetscMalloc1(len,&body);CHKERRQ(ierr);
1450efc6a03SBarry Smith     ierr = PetscStrcpy(body,"--foo_bar_baz\r\n"
1460efc6a03SBarry Smith                             "Content-Type: application/json\r\n\r\n"
147*5dc0f0a4SBarry Smith                             "{");CHKERRQ(ierr);
148*5dc0f0a4SBarry Smith     ierr = PetscPushJSONValue(body,"title",filename,len);CHKERRQ(ierr);
149*5dc0f0a4SBarry Smith     ierr = PetscStrcat(body,",");CHKERRQ(ierr);
150*5dc0f0a4SBarry Smith     ierr = PetscPushJSONValue(body,"mimeType","text.html",len);CHKERRQ(ierr);
151*5dc0f0a4SBarry Smith     ierr = PetscStrcat(body,",");CHKERRQ(ierr);
152*5dc0f0a4SBarry Smith     ierr = PetscPushJSONValue(body,"description","a file",len);CHKERRQ(ierr);
153*5dc0f0a4SBarry Smith     ierr = PetscStrcat(body,"}\r\n\r\n"
1540efc6a03SBarry Smith                             "--foo_bar_baz\r\n"
1550efc6a03SBarry Smith                             "Content-Type: text/html\r\n\r\n");
1560efc6a03SBarry Smith     ierr = PetscStrlen(body,&blen);CHKERRQ(ierr);
1570efc6a03SBarry Smith     fd = fopen (filename, "r");
1580efc6a03SBarry Smith     if (!fd) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to open file: %s",filename);
1590efc6a03SBarry Smith     rd = fread (body+blen, sizeof (unsigned char), sb.st_size, fd);
160d8dcb26dSBarry Smith     if (rd != (size_t) sb.st_size) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to read entire file: %s %d %d",filename,(int)rd,sb.st_size);
1610efc6a03SBarry Smith     fclose(fd);
1620efc6a03SBarry Smith     body[blen + rd] = 0;
1630efc6a03SBarry Smith     ierr = PetscStrcat(body,"\r\n\r\n"
1640efc6a03SBarry Smith                             "--foo_bar_baz\r\n");
1650efc6a03SBarry Smith     ierr = PetscSSLInitializeContext(&ctx);CHKERRQ(ierr);
1660efc6a03SBarry Smith     ierr = PetscHTTPSConnect("www.googleapis.com",443,ctx,&sock,&ssl);CHKERRQ(ierr);
16793e1d32fSBarry Smith     ierr = PetscHTTPSRequest("POST","www.googleapis.com/upload/drive/v2/files/",head,"multipart/related; boundary=\"foo_bar_baz\"",body,ssl,buff,sizeof(buff));CHKERRQ(ierr);
1680efc6a03SBarry Smith     ierr = PetscFree(body);CHKERRQ(ierr);
1690efc6a03SBarry Smith     ierr = PetscSSLDestroyContext(ctx);CHKERRQ(ierr);
1700efc6a03SBarry Smith     close(sock);
1710efc6a03SBarry Smith     ierr   = PetscStrstr(buff,"\"title\"",&title);CHKERRQ(ierr);
1720efc6a03SBarry Smith     if (!title) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Upload of file %s failed",filename);
1730efc6a03SBarry Smith   }
1740efc6a03SBarry Smith   PetscFunctionReturn(0);
1750efc6a03SBarry Smith }
1760efc6a03SBarry Smith 
17768e69593SBarry Smith #if defined(PETSC_HAVE_UNISTD_H)
17868e69593SBarry Smith #include <unistd.h>
17968e69593SBarry Smith #endif
18068e69593SBarry Smith 
1810efc6a03SBarry Smith #undef __FUNCT__
1820efc6a03SBarry Smith #define __FUNCT__ "PetscGoogleDriveAuthorize"
1830efc6a03SBarry Smith /*@C
1840efc6a03SBarry Smith      PetscGoogleDriveAuthorize - Get authorization and refresh token for accessing Google drive from PETSc
1850efc6a03SBarry Smith 
1860efc6a03SBarry Smith    Not collective, only the first process in MPI_Comm does anything
1870efc6a03SBarry Smith 
1880efc6a03SBarry Smith    Input Parameters:
1890efc6a03SBarry Smith +  comm - the MPI communicator
1900efc6a03SBarry Smith -  tokensize - size of the token arrays
1910efc6a03SBarry Smith 
1920efc6a03SBarry Smith    Output Parameters:
1930efc6a03SBarry Smith +  access_token - can be used with PetscGoogleDriveUpload() for this one session
1940efc6a03SBarry Smith -  refresh_token - can be used for ever to obtain new access_tokens with PetscGoogleDriveRefresh(), guard this like a password
1950efc6a03SBarry Smith                    it gives access to your Google Drive
1960efc6a03SBarry Smith 
1970efc6a03SBarry Smith    Notes: This call requires stdout and stdin access from process 0 on the MPI communicator
1980efc6a03SBarry Smith 
199c245270aSBarry Smith    You can run src/sys/webclient/examples/tutorials/googleobtainrefreshtoken to get a refresh token and then in the future pass it to
2004a285bdaSBarry Smith    PETSc programs with -google_refresh_token XXX
2010efc6a03SBarry Smith 
2020efc6a03SBarry Smith .seealso: PetscGoogleDriveRefresh(), PetscGoogleDriveUpload(), PetscURLShorten()
2030efc6a03SBarry Smith 
2040efc6a03SBarry Smith @*/
2050efc6a03SBarry Smith PetscErrorCode PetscGoogleDriveAuthorize(MPI_Comm comm,char access_token[],char refresh_token[],size_t tokensize)
2060efc6a03SBarry Smith {
2070efc6a03SBarry Smith   SSL_CTX        *ctx;
2080efc6a03SBarry Smith   SSL            *ssl;
2090efc6a03SBarry Smith   int            sock;
2100efc6a03SBarry Smith   PetscErrorCode ierr;
211*5dc0f0a4SBarry Smith   char           buff[8*1024],*ptr,body[1024];
2120efc6a03SBarry Smith   PetscMPIInt    rank;
2130efc6a03SBarry Smith   size_t         len;
214*5dc0f0a4SBarry Smith   PetscBool      found;
2150efc6a03SBarry Smith 
2160efc6a03SBarry Smith   PetscFunctionBegin;
21768e69593SBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
21868e69593SBarry Smith   if (!rank) {
21968e69593SBarry Smith     if (!isatty(fileno(PETSC_STDOUT))) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Requires users input/output");
2204a285bdaSBarry Smith     ierr = PetscPrintf(comm,"Cut and paste the following into your browser:\n\n"
2210efc6a03SBarry Smith                             "https://accounts.google.com/o/oauth2/auth?"
2220efc6a03SBarry Smith                             "scope=https%%3A%%2F%%2Fwww.googleapis.com%%2Fauth%%2Fdrive.file&"
2230efc6a03SBarry Smith                             "redirect_uri=urn:ietf:wg:oauth:2.0:oob&"
2240efc6a03SBarry Smith                             "response_type=code&"
2250efc6a03SBarry Smith                             "client_id="
2260efc6a03SBarry Smith                             PETSC_GOOGLE_CLIENT_ID
2270efc6a03SBarry Smith                             "\n\n");CHKERRQ(ierr);
2280efc6a03SBarry Smith     ierr = PetscPrintf(comm,"Paste the result here:");CHKERRQ(ierr);
2290efc6a03SBarry Smith     ptr  = fgets(buff, 1024, stdin);
2300efc6a03SBarry Smith     if (!ptr) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_FILE_READ, "Error reading from stdin: %d", errno);
2310efc6a03SBarry Smith     ierr = PetscStrlen(buff,&len);CHKERRQ(ierr);
2320efc6a03SBarry Smith     buff[len-1] = 0; /* remove carriage return at end of line */
2330efc6a03SBarry Smith 
2340efc6a03SBarry Smith     ierr = PetscSSLInitializeContext(&ctx);CHKERRQ(ierr);
2350efc6a03SBarry Smith     ierr = PetscHTTPSConnect("accounts.google.com",443,ctx,&sock,&ssl);CHKERRQ(ierr);
2360efc6a03SBarry Smith     ierr = PetscStrcpy(body,"code=");CHKERRQ(ierr);
2370efc6a03SBarry Smith     ierr = PetscStrcat(body,buff);CHKERRQ(ierr);
2380efc6a03SBarry Smith     ierr = PetscStrcat(body,"&client_id=");CHKERRQ(ierr);
2390efc6a03SBarry Smith     ierr = PetscStrcat(body,PETSC_GOOGLE_CLIENT_ID);CHKERRQ(ierr);
2400efc6a03SBarry Smith     ierr = PetscStrcat(body,"&client_secret=");CHKERRQ(ierr);
2410efc6a03SBarry Smith     ierr = PetscStrcat(body,PETSC_GOOGLE_CLIENT_ST);CHKERRQ(ierr);
2420efc6a03SBarry Smith     ierr = PetscStrcat(body,"&redirect_uri=urn:ietf:wg:oauth:2.0:oob&");CHKERRQ(ierr);
2430efc6a03SBarry Smith     ierr = PetscStrcat(body,"grant_type=authorization_code");CHKERRQ(ierr);
2440efc6a03SBarry Smith 
24593e1d32fSBarry Smith     ierr = PetscHTTPSRequest("POST","accounts.google.com/o/oauth2/token",NULL,"application/x-www-form-urlencoded",body,ssl,buff,sizeof(buff));CHKERRQ(ierr);
2460efc6a03SBarry Smith     ierr = PetscSSLDestroyContext(ctx);CHKERRQ(ierr);
2470efc6a03SBarry Smith     close(sock);
2480efc6a03SBarry Smith 
249*5dc0f0a4SBarry Smith     ierr   = PetscPullJSONValue(buff,"access_token",access_token,tokensize,&found);CHKERRQ(ierr);
250*5dc0f0a4SBarry Smith     if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Google drive did not return access_token");
251*5dc0f0a4SBarry Smith     ierr   = PetscPullJSONValue(buff,"refresh_token",refresh_token,tokensize,&found);CHKERRQ(ierr);
252*5dc0f0a4SBarry Smith     if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Google drive did not return refresh_token");
2530efc6a03SBarry Smith 
2540efc6a03SBarry Smith     ierr = PetscPrintf(comm,"Here is your Google refresh token, save it in a save place, in the future you can run PETSc\n");CHKERRQ(ierr);
255*5dc0f0a4SBarry Smith     ierr = PetscPrintf(comm,"programs with the option -google_refresh_token %s\n",refresh_token);CHKERRQ(ierr);
2560efc6a03SBarry Smith     ierr = PetscPrintf(comm,"to access Google Drive automatically\n");CHKERRQ(ierr);
2570efc6a03SBarry Smith   }
2580efc6a03SBarry Smith   PetscFunctionReturn(0);
2590efc6a03SBarry Smith }
2600efc6a03SBarry Smith 
2610efc6a03SBarry Smith 
2620efc6a03SBarry Smith #undef __FUNCT__
2630efc6a03SBarry Smith #define __FUNCT__ "PetscURLShorten"
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 
2740efc6a03SBarry Smith .seealso: PetscGoogleDriveRefresh(), PetscGoogleDriveUpload(), PetscGoogleDriveAuthorize()
2750efc6a03SBarry Smith @*/
2760efc6a03SBarry Smith PetscErrorCode PetscURLShorten(const char url[],char shorturl[],size_t lenshorturl)
2770efc6a03SBarry Smith {
2780efc6a03SBarry Smith   SSL_CTX        *ctx;
2790efc6a03SBarry Smith   SSL            *ssl;
2800efc6a03SBarry Smith   int            sock;
2810efc6a03SBarry Smith   PetscErrorCode ierr;
282*5dc0f0a4SBarry Smith   char           buff[1024],body[512];
283*5dc0f0a4SBarry Smith   PetscBool      found;
2840efc6a03SBarry Smith 
2850efc6a03SBarry Smith   PetscFunctionBegin;
2860efc6a03SBarry Smith   ierr = PetscSSLInitializeContext(&ctx);CHKERRQ(ierr);
2870efc6a03SBarry Smith   ierr = PetscHTTPSConnect("www.googleapis.com",443,ctx,&sock,&ssl);CHKERRQ(ierr);
288*5dc0f0a4SBarry Smith   ierr = PetscStrcpy(body,"{");CHKERRQ(ierr);
289*5dc0f0a4SBarry Smith   ierr = PetscPushJSONValue(body,"longUrl",url,sizeof(body)-2);CHKERRQ(ierr);
290*5dc0f0a4SBarry Smith   ierr = PetscStrcat(body,"}");CHKERRQ(ierr);
29193e1d32fSBarry Smith   ierr = PetscHTTPSRequest("POST","www.googleapis.com/urlshortener/v1/url",NULL,"application/json",body,ssl,buff,sizeof(buff));CHKERRQ(ierr);
2920efc6a03SBarry Smith   ierr = PetscSSLDestroyContext(ctx);CHKERRQ(ierr);
2930efc6a03SBarry Smith   close(sock);
294*5dc0f0a4SBarry Smith 
295*5dc0f0a4SBarry Smith   ierr   = PetscPullJSONValue(buff,"id",shorturl,lenshorturl,&found);CHKERRQ(ierr);
296*5dc0f0a4SBarry Smith   if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Google drive did not return short URL");
2970efc6a03SBarry Smith   PetscFunctionReturn(0);
2980efc6a03SBarry Smith }
2990efc6a03SBarry Smith 
300