xref: /petsc/src/sys/webclient/google.c (revision 28b400f66ebc7ae0049166a2294dfcd3df27e64b)
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 
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:
3210699b91SBarry Smith .  -google_refresh_token XXX - where XXX was obtained from PetscGoogleDriveAuthorize()
334a285bdaSBarry Smith 
342b26979fSBarry Smith    Level: intermediate
354a285bdaSBarry Smith 
360efc6a03SBarry Smith .seealso: PetscURLShorten(), PetscGoogleDriveAuthorize(), PetscGoogleDriveUpload()
370efc6a03SBarry Smith 
380efc6a03SBarry Smith @*/
390efc6a03SBarry Smith PetscErrorCode PetscGoogleDriveRefresh(MPI_Comm comm,const char refresh_token[],char access_token[],size_t tokensize)
400efc6a03SBarry Smith {
410efc6a03SBarry Smith   SSL_CTX        *ctx;
420efc6a03SBarry Smith   SSL            *ssl;
430efc6a03SBarry Smith   int            sock;
440efc6a03SBarry Smith   PetscErrorCode ierr;
455dc0f0a4SBarry Smith   char           buff[8*1024],body[1024];
460efc6a03SBarry Smith   PetscMPIInt    rank;
470efc6a03SBarry Smith   char           *refreshtoken = (char*)refresh_token;
485dc0f0a4SBarry Smith   PetscBool      found;
490efc6a03SBarry Smith 
500efc6a03SBarry Smith   PetscFunctionBegin;
515f80ce2aSJacob Faibussowitsch   CHKERRMPI(MPI_Comm_rank(comm,&rank));
52dd400576SPatrick Sanan   if (rank == 0) {
530efc6a03SBarry Smith     if (!refresh_token) {
540efc6a03SBarry Smith       PetscBool set;
555f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscMalloc1(512,&refreshtoken));
565f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscOptionsGetString(NULL,NULL,"-google_refresh_token",refreshtoken,sizeof(refreshtoken),&set));
570efc6a03SBarry Smith       if (!set) {
585f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscGoogleDriveAuthorize(comm,access_token,refreshtoken,512*sizeof(char)));
595f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscFree(refreshtoken));
600efc6a03SBarry Smith         PetscFunctionReturn(0);
610efc6a03SBarry Smith       }
620efc6a03SBarry Smith     }
635f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSSLInitializeContext(&ctx));
645f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscHTTPSConnect("accounts.google.com",443,ctx,&sock,&ssl));
655f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrcpy(body,"client_id="));
665f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrcat(body,PETSC_GOOGLE_CLIENT_ID));
675f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrcat(body,"&client_secret="));
685f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrcat(body,PETSC_GOOGLE_CLIENT_ST));
695f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrcat(body,"&refresh_token="));
705f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrcat(body,refreshtoken));
715f80ce2aSJacob Faibussowitsch     if (!refresh_token) CHKERRQ(PetscFree(refreshtoken));
725f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrcat(body,"&grant_type=refresh_token"));
730efc6a03SBarry Smith 
745f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscHTTPSRequest("POST","accounts.google.com/o/oauth2/token",NULL,"application/x-www-form-urlencoded",body,ssl,buff,sizeof(buff)));
755f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSSLDestroyContext(ctx));
760efc6a03SBarry Smith     close(sock);
770efc6a03SBarry Smith 
785f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscPullJSONValue(buff,"access_token",access_token,tokensize,&found));
79*28b400f6SJacob Faibussowitsch     PetscCheck(found,PETSC_COMM_SELF,PETSC_ERR_LIB,"Google drive did not return access_token");
800efc6a03SBarry Smith   }
810efc6a03SBarry Smith   PetscFunctionReturn(0);
820efc6a03SBarry Smith }
830efc6a03SBarry Smith 
840efc6a03SBarry Smith #include <sys/stat.h>
850efc6a03SBarry Smith 
860efc6a03SBarry Smith /*@C
870efc6a03SBarry Smith      PetscGoogleDriveUpload - Loads a file to the Google Drive
880efc6a03SBarry Smith 
890efc6a03SBarry Smith      Not collective, only the first process in the MPI_Comm uploads the file
900efc6a03SBarry Smith 
910efc6a03SBarry Smith   Input Parameters:
920efc6a03SBarry Smith +   comm - MPI communicator
930efc6a03SBarry Smith .   access_token - obtained with PetscGoogleDriveRefresh(), pass NULL to have PETSc generate one
940efc6a03SBarry Smith -   filename - file to upload; if you upload multiple times it will have different names each time on Google Drive
950efc6a03SBarry Smith 
960efc6a03SBarry Smith   Options Database:
97147403d9SBarry Smith .  -google_refresh_token XXX - pass the access token for the operation
980efc6a03SBarry Smith 
990efc6a03SBarry Smith   Usage Patterns:
1004a285bdaSBarry Smith     With PETSc option -google_refresh_token  XXX given
1010efc6a03SBarry Smith     PetscGoogleDriveUpload(comm,NULL,filename);        will upload file with no user interaction
1020efc6a03SBarry Smith 
1034a285bdaSBarry Smith     Without PETSc option -google_refresh_token XXX given
1044683183fSBarry Smith     PetscGoogleDriveUpload(comm,NULL,filename);        for first use will prompt user to authorize access to Google Drive with their browser
1050efc6a03SBarry Smith 
1064a285bdaSBarry Smith     With PETSc option -google_refresh_token  XXX given
1070efc6a03SBarry Smith     PetscGoogleDriveRefresh(comm,NULL,access_token,sizeof(access_token));
1080efc6a03SBarry Smith     PetscGoogleDriveUpload(comm,access_token,filename);
1090efc6a03SBarry Smith 
1100efc6a03SBarry Smith     With refresh token entered in some way by the user
1110efc6a03SBarry Smith     PetscGoogleDriveRefresh(comm,refresh_token,access_token,sizeof(access_token));
1120efc6a03SBarry Smith     PetscGoogleDriveUpload(comm,access_token,filename);
1130efc6a03SBarry Smith 
1140efc6a03SBarry Smith     PetscGoogleDriveAuthorize(comm,access_token,refresh_token,sizeof(access_token));
1150efc6a03SBarry Smith     PetscGoogleDriveUpload(comm,access_token,filename);
1160efc6a03SBarry Smith 
1172b26979fSBarry Smith    Level: intermediate
1182b26979fSBarry Smith 
1190efc6a03SBarry Smith .seealso: PetscURLShorten(), PetscGoogleDriveAuthorize(), PetscGoogleDriveRefresh()
1200efc6a03SBarry Smith 
1210efc6a03SBarry Smith @*/
1220efc6a03SBarry Smith PetscErrorCode PetscGoogleDriveUpload(MPI_Comm comm,const char access_token[],const char filename[])
1230efc6a03SBarry Smith {
1240efc6a03SBarry Smith   SSL_CTX        *ctx;
1250efc6a03SBarry Smith   SSL            *ssl;
1260efc6a03SBarry Smith   int            sock;
1270efc6a03SBarry Smith   PetscErrorCode ierr;
1280efc6a03SBarry Smith   char           head[1024],buff[8*1024],*body,*title;
1290efc6a03SBarry Smith   PetscMPIInt    rank;
1300efc6a03SBarry Smith   struct stat    sb;
1310efc6a03SBarry Smith   size_t         len,blen,rd;
1320efc6a03SBarry Smith   FILE           *fd;
1332da392ccSBarry Smith   int            err;
1340efc6a03SBarry Smith 
1350efc6a03SBarry Smith   PetscFunctionBegin;
1365f80ce2aSJacob Faibussowitsch   CHKERRMPI(MPI_Comm_rank(comm,&rank));
137dd400576SPatrick Sanan   if (rank == 0) {
1385f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrcpy(head,"Authorization: Bearer "));
1395f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrcat(head,access_token));
1405f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrcat(head,"\r\n"));
1415f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrcat(head,"uploadType: multipart\r\n"));
1420efc6a03SBarry Smith 
1432da392ccSBarry Smith     err = stat(filename,&sb);
144*28b400f6SJacob Faibussowitsch     PetscCheck(!err,PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to stat file: %s",filename);
1450efc6a03SBarry Smith     len = 1024 + sb.st_size;
1465f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscMalloc1(len,&body));
1470efc6a03SBarry Smith     ierr = PetscStrcpy(body,"--foo_bar_baz\r\n"
1480efc6a03SBarry Smith                             "Content-Type: application/json\r\n\r\n"
1495dc0f0a4SBarry Smith                             "{");CHKERRQ(ierr);
1505f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscPushJSONValue(body,"title",filename,len));
1515f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrcat(body,","));
1525f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscPushJSONValue(body,"mimeType","text.html",len));
1535f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrcat(body,","));
1545f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscPushJSONValue(body,"description","a file",len));
1555dc0f0a4SBarry Smith     ierr = PetscStrcat(body,"}\r\n\r\n"
1560efc6a03SBarry Smith                             "--foo_bar_baz\r\n"
157302440fdSBarry Smith                             "Content-Type: text/html\r\n\r\n");CHKERRQ(ierr);
1585f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrlen(body,&blen));
1590efc6a03SBarry Smith     fd = fopen (filename, "r");
160*28b400f6SJacob Faibussowitsch     PetscCheck(fd,PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to open file: %s",filename);
1610efc6a03SBarry Smith     rd = fread (body+blen, sizeof (unsigned char), sb.st_size, fd);
1622c71b3e2SJacob Faibussowitsch     PetscCheckFalse(rd != (size_t) sb.st_size,PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to read entire file: %s %d %d",filename,(int)rd,sb.st_size);
1630efc6a03SBarry Smith     fclose(fd);
1640efc6a03SBarry Smith     body[blen + rd] = 0;
1650efc6a03SBarry Smith     ierr = PetscStrcat(body,"\r\n\r\n"
166302440fdSBarry Smith                             "--foo_bar_baz\r\n");CHKERRQ(ierr);
1675f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSSLInitializeContext(&ctx));
1685f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscHTTPSConnect("www.googleapis.com",443,ctx,&sock,&ssl));
1695f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscHTTPSRequest("POST","www.googleapis.com/upload/drive/v2/files/",head,"multipart/related; boundary=\"foo_bar_baz\"",body,ssl,buff,sizeof(buff)));
1705f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscFree(body));
1715f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSSLDestroyContext(ctx));
1720efc6a03SBarry Smith     close(sock);
1735f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrstr(buff,"\"title\"",&title));
174*28b400f6SJacob Faibussowitsch     PetscCheck(title,PETSC_COMM_SELF,PETSC_ERR_LIB,"Upload of file %s failed",filename);
1750efc6a03SBarry Smith   }
1760efc6a03SBarry Smith   PetscFunctionReturn(0);
1770efc6a03SBarry Smith }
1780efc6a03SBarry Smith 
17968e69593SBarry Smith #if defined(PETSC_HAVE_UNISTD_H)
18068e69593SBarry Smith #include <unistd.h>
18168e69593SBarry Smith #endif
18268e69593SBarry Smith 
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 
19795452b02SPatrick Sanan    Notes:
19895452b02SPatrick Sanan     This call requires stdout and stdin access from process 0 on the MPI communicator
1990efc6a03SBarry Smith 
200c4762a1bSJed Brown    You can run src/sys/webclient/tutorials/googleobtainrefreshtoken to get a refresh token and then in the future pass it to
2014a285bdaSBarry Smith    PETSc programs with -google_refresh_token XXX
2020efc6a03SBarry Smith 
2032b26979fSBarry Smith    Level: intermediate
2042b26979fSBarry Smith 
2050efc6a03SBarry Smith .seealso: PetscGoogleDriveRefresh(), PetscGoogleDriveUpload(), PetscURLShorten()
2060efc6a03SBarry Smith 
2070efc6a03SBarry Smith @*/
2080efc6a03SBarry Smith PetscErrorCode PetscGoogleDriveAuthorize(MPI_Comm comm,char access_token[],char refresh_token[],size_t tokensize)
2090efc6a03SBarry Smith {
2100efc6a03SBarry Smith   SSL_CTX        *ctx;
2110efc6a03SBarry Smith   SSL            *ssl;
2120efc6a03SBarry Smith   int            sock;
2130efc6a03SBarry Smith   PetscErrorCode ierr;
2145dc0f0a4SBarry Smith   char           buff[8*1024],*ptr,body[1024];
2150efc6a03SBarry Smith   PetscMPIInt    rank;
2160efc6a03SBarry Smith   size_t         len;
2175dc0f0a4SBarry Smith   PetscBool      found;
2180efc6a03SBarry Smith 
2190efc6a03SBarry Smith   PetscFunctionBegin;
2205f80ce2aSJacob Faibussowitsch   CHKERRMPI(MPI_Comm_rank(comm,&rank));
221dd400576SPatrick Sanan   if (rank == 0) {
2222c71b3e2SJacob Faibussowitsch     PetscCheckFalse(!isatty(fileno(PETSC_STDOUT)),PETSC_COMM_SELF,PETSC_ERR_USER,"Requires users input/output");
2234a285bdaSBarry Smith     ierr = PetscPrintf(comm,"Cut and paste the following into your browser:\n\n"
2240efc6a03SBarry Smith                             "https://accounts.google.com/o/oauth2/auth?"
2250efc6a03SBarry Smith                             "scope=https%%3A%%2F%%2Fwww.googleapis.com%%2Fauth%%2Fdrive.file&"
2260efc6a03SBarry Smith                             "redirect_uri=urn:ietf:wg:oauth:2.0:oob&"
2270efc6a03SBarry Smith                             "response_type=code&"
2280efc6a03SBarry Smith                             "client_id="
2290efc6a03SBarry Smith                             PETSC_GOOGLE_CLIENT_ID
2300efc6a03SBarry Smith                             "\n\n");CHKERRQ(ierr);
2315f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscPrintf(comm,"Paste the result here:"));
2320efc6a03SBarry Smith     ptr  = fgets(buff, 1024, stdin);
233*28b400f6SJacob Faibussowitsch     PetscCheck(ptr,PETSC_COMM_SELF, PETSC_ERR_FILE_READ, "Error reading from stdin: %d", errno);
2345f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrlen(buff,&len));
2350efc6a03SBarry Smith     buff[len-1] = 0; /* remove carriage return at end of line */
2360efc6a03SBarry Smith 
2375f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSSLInitializeContext(&ctx));
2385f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscHTTPSConnect("accounts.google.com",443,ctx,&sock,&ssl));
2395f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrcpy(body,"code="));
2405f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrcat(body,buff));
2415f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrcat(body,"&client_id="));
2425f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrcat(body,PETSC_GOOGLE_CLIENT_ID));
2435f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrcat(body,"&client_secret="));
2445f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrcat(body,PETSC_GOOGLE_CLIENT_ST));
2455f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrcat(body,"&redirect_uri=urn:ietf:wg:oauth:2.0:oob&"));
2465f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrcat(body,"grant_type=authorization_code"));
2470efc6a03SBarry Smith 
2485f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscHTTPSRequest("POST","accounts.google.com/o/oauth2/token",NULL,"application/x-www-form-urlencoded",body,ssl,buff,sizeof(buff)));
2495f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSSLDestroyContext(ctx));
2500efc6a03SBarry Smith     close(sock);
2510efc6a03SBarry Smith 
2525f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscPullJSONValue(buff,"access_token",access_token,tokensize,&found));
253*28b400f6SJacob Faibussowitsch     PetscCheck(found,PETSC_COMM_SELF,PETSC_ERR_LIB,"Google drive did not return access_token");
2545f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscPullJSONValue(buff,"refresh_token",refresh_token,tokensize,&found));
255*28b400f6SJacob Faibussowitsch     PetscCheck(found,PETSC_COMM_SELF,PETSC_ERR_LIB,"Google drive did not return refresh_token");
2560efc6a03SBarry Smith 
2575f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscPrintf(comm,"Here is your Google refresh token, save it in a save place, in the future you can run PETSc\n"));
2585f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscPrintf(comm,"programs with the option -google_refresh_token %s\n",refresh_token));
2595f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscPrintf(comm,"to access Google Drive automatically\n"));
2600efc6a03SBarry Smith   }
2610efc6a03SBarry Smith   PetscFunctionReturn(0);
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 
2760efc6a03SBarry Smith .seealso: PetscGoogleDriveRefresh(), PetscGoogleDriveUpload(), PetscGoogleDriveAuthorize()
2770efc6a03SBarry Smith @*/
2780efc6a03SBarry Smith PetscErrorCode PetscURLShorten(const char url[],char shorturl[],size_t lenshorturl)
2790efc6a03SBarry Smith {
2800efc6a03SBarry Smith   SSL_CTX        *ctx;
2810efc6a03SBarry Smith   SSL            *ssl;
2820efc6a03SBarry Smith   int            sock;
2830efc6a03SBarry Smith   PetscErrorCode ierr;
2845708bc22SBarry Smith   char           buff[1024],body[512],post[1024];
2855dc0f0a4SBarry Smith   PetscBool      found;
2860efc6a03SBarry Smith 
2870efc6a03SBarry Smith   PetscFunctionBegin;
2885f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSSLInitializeContext(&ctx));
2895f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscHTTPSConnect("www.googleapis.com",443,ctx,&sock,&ssl));
2905f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscStrcpy(body,"{"));
2915f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscPushJSONValue(body,"longUrl",url,sizeof(body)-2));
2925f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscStrcat(body,"}"));
2935f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSNPrintf(post,sizeof(post),"www.googleapis.com/urlshortener/v1/url?key=%s",PETSC_GOOGLE_API_KEY));
2945f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscHTTPSRequest("POST",post,NULL,"application/json",body,ssl,buff,sizeof(buff)));
2955f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSSLDestroyContext(ctx));
2960efc6a03SBarry Smith   close(sock);
2975dc0f0a4SBarry Smith 
2985f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscPullJSONValue(buff,"id",shorturl,lenshorturl,&found));
299*28b400f6SJacob Faibussowitsch   PetscCheck(found,PETSC_COMM_SELF,PETSC_ERR_LIB,"Google drive did not return short URL");
3000efc6a03SBarry Smith   PetscFunctionReturn(0);
3010efc6a03SBarry Smith }
302