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 90efc6a03SBarry Smith See - http://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" 150efc6a03SBarry Smith 160efc6a03SBarry Smith 170efc6a03SBarry Smith #undef __FUNCT__ 180efc6a03SBarry Smith #define __FUNCT__ "PetscGoogleDriveRefresh" 190efc6a03SBarry Smith /*@C 200efc6a03SBarry Smith PetscGoogleDriveRefresh - Get a new authorization token for accessing Google drive from PETSc from a refresh token 210efc6a03SBarry Smith 220efc6a03SBarry Smith Not collective, only the first process in the MPI_Comm does anything 230efc6a03SBarry Smith 240efc6a03SBarry Smith Input Parameters: 250efc6a03SBarry Smith + comm - MPI communicator 260efc6a03SBarry Smith . refresh token - obtained with PetscGoogleDriveAuthorize(), if NULL PETSc will first look for one in the options data 270efc6a03SBarry Smith if not found it will call PetscGoogleDriveAuthorize() 280efc6a03SBarry Smith - tokensize - size of the output string access_token 290efc6a03SBarry Smith 300efc6a03SBarry Smith Output Parameter: 310efc6a03SBarry Smith . access_token - token that can be passed to PetscGoogleDriveUpload() 320efc6a03SBarry Smith 334a285bdaSBarry Smith Options Database: 344a285bdaSBarry Smith . -google_refresh_token XXX where XXX was obtained from PetscGoogleDriveAuthorize() 354a285bdaSBarry Smith 36*2b26979fSBarry Smith Level: intermediate 374a285bdaSBarry Smith 380efc6a03SBarry Smith .seealso: PetscURLShorten(), PetscGoogleDriveAuthorize(), PetscGoogleDriveUpload() 390efc6a03SBarry Smith 400efc6a03SBarry Smith @*/ 410efc6a03SBarry Smith PetscErrorCode PetscGoogleDriveRefresh(MPI_Comm comm,const char refresh_token[],char access_token[],size_t tokensize) 420efc6a03SBarry Smith { 430efc6a03SBarry Smith SSL_CTX *ctx; 440efc6a03SBarry Smith SSL *ssl; 450efc6a03SBarry Smith int sock; 460efc6a03SBarry Smith PetscErrorCode ierr; 475dc0f0a4SBarry Smith char buff[8*1024],body[1024]; 480efc6a03SBarry Smith PetscMPIInt rank; 490efc6a03SBarry Smith char *refreshtoken = (char*)refresh_token; 505dc0f0a4SBarry Smith PetscBool found; 510efc6a03SBarry Smith 520efc6a03SBarry Smith PetscFunctionBegin; 530efc6a03SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 540efc6a03SBarry Smith if (!rank) { 550efc6a03SBarry Smith if (!refresh_token) { 560efc6a03SBarry Smith PetscBool set; 570efc6a03SBarry Smith ierr = PetscMalloc1(512,&refreshtoken);CHKERRQ(ierr); 584a285bdaSBarry Smith ierr = PetscOptionsGetString(NULL,"-google_refresh_token",refreshtoken,512,&set);CHKERRQ(ierr); 590efc6a03SBarry Smith if (!set) { 600efc6a03SBarry Smith ierr = PetscGoogleDriveAuthorize(comm,access_token,refreshtoken,512*sizeof(char));CHKERRQ(ierr); 610efc6a03SBarry Smith ierr = PetscFree(refreshtoken);CHKERRQ(ierr); 620efc6a03SBarry Smith PetscFunctionReturn(0); 630efc6a03SBarry Smith } 640efc6a03SBarry Smith } 650efc6a03SBarry Smith ierr = PetscSSLInitializeContext(&ctx);CHKERRQ(ierr); 660efc6a03SBarry Smith ierr = PetscHTTPSConnect("accounts.google.com",443,ctx,&sock,&ssl);CHKERRQ(ierr); 674a285bdaSBarry Smith ierr = PetscStrcpy(body,"client_id=");CHKERRQ(ierr); 680efc6a03SBarry Smith ierr = PetscStrcat(body,PETSC_GOOGLE_CLIENT_ID);CHKERRQ(ierr); 690efc6a03SBarry Smith ierr = PetscStrcat(body,"&client_secret=");CHKERRQ(ierr); 700efc6a03SBarry Smith ierr = PetscStrcat(body,PETSC_GOOGLE_CLIENT_ST);CHKERRQ(ierr); 710efc6a03SBarry Smith ierr = PetscStrcat(body,"&refresh_token=");CHKERRQ(ierr); 720efc6a03SBarry Smith ierr = PetscStrcat(body,refreshtoken);CHKERRQ(ierr); 730efc6a03SBarry Smith if (!refresh_token) {ierr = PetscFree(refreshtoken);CHKERRQ(ierr);} 740efc6a03SBarry Smith ierr = PetscStrcat(body,"&grant_type=refresh_token");CHKERRQ(ierr); 750efc6a03SBarry Smith 7693e1d32fSBarry Smith ierr = PetscHTTPSRequest("POST","accounts.google.com/o/oauth2/token",NULL,"application/x-www-form-urlencoded",body,ssl,buff,sizeof(buff));CHKERRQ(ierr); 770efc6a03SBarry Smith ierr = PetscSSLDestroyContext(ctx);CHKERRQ(ierr); 780efc6a03SBarry Smith close(sock); 790efc6a03SBarry Smith 805dc0f0a4SBarry Smith ierr = PetscPullJSONValue(buff,"access_token",access_token,tokensize,&found);CHKERRQ(ierr); 815dc0f0a4SBarry Smith if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Google drive did not return access_token"); 820efc6a03SBarry Smith } 830efc6a03SBarry Smith PetscFunctionReturn(0); 840efc6a03SBarry Smith } 850efc6a03SBarry Smith 860efc6a03SBarry Smith #include <sys/stat.h> 870efc6a03SBarry Smith 880efc6a03SBarry Smith #undef __FUNCT__ 890efc6a03SBarry Smith #define __FUNCT__ "PetscGoogleDriveUpload" 900efc6a03SBarry Smith /*@C 910efc6a03SBarry Smith PetscGoogleDriveUpload - Loads a file to the Google Drive 920efc6a03SBarry Smith 930efc6a03SBarry Smith Not collective, only the first process in the MPI_Comm uploads the file 940efc6a03SBarry Smith 950efc6a03SBarry Smith Input Parameters: 960efc6a03SBarry Smith + comm - MPI communicator 970efc6a03SBarry Smith . access_token - obtained with PetscGoogleDriveRefresh(), pass NULL to have PETSc generate one 980efc6a03SBarry Smith - filename - file to upload; if you upload multiple times it will have different names each time on Google Drive 990efc6a03SBarry Smith 1000efc6a03SBarry Smith Options Database: 1014a285bdaSBarry Smith . -google_refresh_token XXX 1020efc6a03SBarry Smith 1030efc6a03SBarry Smith Usage Patterns: 1044a285bdaSBarry Smith With PETSc option -google_refresh_token XXX given 1050efc6a03SBarry Smith PetscGoogleDriveUpload(comm,NULL,filename); will upload file with no user interaction 1060efc6a03SBarry Smith 1074a285bdaSBarry Smith Without PETSc option -google_refresh_token XXX given 1080efc6a03SBarry Smith PetscGoogleDriveUpload(comm,NULL,filename); for first use will prompt user to authorize access to Google Drive with their processor 1090efc6a03SBarry Smith 1104a285bdaSBarry Smith With PETSc option -google_refresh_token XXX given 1110efc6a03SBarry Smith PetscGoogleDriveRefresh(comm,NULL,access_token,sizeof(access_token)); 1120efc6a03SBarry Smith PetscGoogleDriveUpload(comm,access_token,filename); 1130efc6a03SBarry Smith 1140efc6a03SBarry Smith With refresh token entered in some way by the user 1150efc6a03SBarry Smith PetscGoogleDriveRefresh(comm,refresh_token,access_token,sizeof(access_token)); 1160efc6a03SBarry Smith PetscGoogleDriveUpload(comm,access_token,filename); 1170efc6a03SBarry Smith 1180efc6a03SBarry Smith PetscGoogleDriveAuthorize(comm,access_token,refresh_token,sizeof(access_token)); 1190efc6a03SBarry Smith PetscGoogleDriveUpload(comm,access_token,filename); 1200efc6a03SBarry Smith 121*2b26979fSBarry Smith Level: intermediate 122*2b26979fSBarry Smith 1230efc6a03SBarry Smith .seealso: PetscURLShorten(), PetscGoogleDriveAuthorize(), PetscGoogleDriveRefresh() 1240efc6a03SBarry Smith 1250efc6a03SBarry Smith @*/ 1260efc6a03SBarry Smith PetscErrorCode PetscGoogleDriveUpload(MPI_Comm comm,const char access_token[],const char filename[]) 1270efc6a03SBarry Smith { 1280efc6a03SBarry Smith SSL_CTX *ctx; 1290efc6a03SBarry Smith SSL *ssl; 1300efc6a03SBarry Smith int sock; 1310efc6a03SBarry Smith PetscErrorCode ierr; 1320efc6a03SBarry Smith char head[1024],buff[8*1024],*body,*title; 1330efc6a03SBarry Smith PetscMPIInt rank; 1340efc6a03SBarry Smith struct stat sb; 1350efc6a03SBarry Smith size_t len,blen,rd; 1360efc6a03SBarry Smith FILE *fd; 1370efc6a03SBarry Smith 1380efc6a03SBarry Smith PetscFunctionBegin; 1390efc6a03SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 1400efc6a03SBarry Smith if (!rank) { 1410efc6a03SBarry Smith ierr = PetscStrcpy(head,"Authorization: Bearer ");CHKERRQ(ierr); 1420efc6a03SBarry Smith ierr = PetscStrcat(head,access_token);CHKERRQ(ierr); 1430efc6a03SBarry Smith ierr = PetscStrcat(head,"\r\n");CHKERRQ(ierr); 1440efc6a03SBarry Smith ierr = PetscStrcat(head,"uploadType: multipart\r\n");CHKERRQ(ierr); 1450efc6a03SBarry Smith 1460efc6a03SBarry Smith ierr = stat(filename,&sb); 1470efc6a03SBarry Smith if (ierr) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to stat file: %s",filename); 1480efc6a03SBarry Smith len = 1024 + sb.st_size; 1490efc6a03SBarry Smith ierr = PetscMalloc1(len,&body);CHKERRQ(ierr); 1500efc6a03SBarry Smith ierr = PetscStrcpy(body,"--foo_bar_baz\r\n" 1510efc6a03SBarry Smith "Content-Type: application/json\r\n\r\n" 1525dc0f0a4SBarry Smith "{");CHKERRQ(ierr); 1535dc0f0a4SBarry Smith ierr = PetscPushJSONValue(body,"title",filename,len);CHKERRQ(ierr); 1545dc0f0a4SBarry Smith ierr = PetscStrcat(body,",");CHKERRQ(ierr); 1555dc0f0a4SBarry Smith ierr = PetscPushJSONValue(body,"mimeType","text.html",len);CHKERRQ(ierr); 1565dc0f0a4SBarry Smith ierr = PetscStrcat(body,",");CHKERRQ(ierr); 1575dc0f0a4SBarry Smith ierr = PetscPushJSONValue(body,"description","a file",len);CHKERRQ(ierr); 1585dc0f0a4SBarry Smith ierr = PetscStrcat(body,"}\r\n\r\n" 1590efc6a03SBarry Smith "--foo_bar_baz\r\n" 1600efc6a03SBarry Smith "Content-Type: text/html\r\n\r\n"); 1610efc6a03SBarry Smith ierr = PetscStrlen(body,&blen);CHKERRQ(ierr); 1620efc6a03SBarry Smith fd = fopen (filename, "r"); 1630efc6a03SBarry Smith if (!fd) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to open file: %s",filename); 1640efc6a03SBarry Smith rd = fread (body+blen, sizeof (unsigned char), sb.st_size, fd); 165d8dcb26dSBarry 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); 1660efc6a03SBarry Smith fclose(fd); 1670efc6a03SBarry Smith body[blen + rd] = 0; 1680efc6a03SBarry Smith ierr = PetscStrcat(body,"\r\n\r\n" 1690efc6a03SBarry Smith "--foo_bar_baz\r\n"); 1700efc6a03SBarry Smith ierr = PetscSSLInitializeContext(&ctx);CHKERRQ(ierr); 1710efc6a03SBarry Smith ierr = PetscHTTPSConnect("www.googleapis.com",443,ctx,&sock,&ssl);CHKERRQ(ierr); 17293e1d32fSBarry 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); 1730efc6a03SBarry Smith ierr = PetscFree(body);CHKERRQ(ierr); 1740efc6a03SBarry Smith ierr = PetscSSLDestroyContext(ctx);CHKERRQ(ierr); 1750efc6a03SBarry Smith close(sock); 1760efc6a03SBarry Smith ierr = PetscStrstr(buff,"\"title\"",&title);CHKERRQ(ierr); 1770efc6a03SBarry Smith if (!title) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Upload of file %s failed",filename); 1780efc6a03SBarry Smith } 1790efc6a03SBarry Smith PetscFunctionReturn(0); 1800efc6a03SBarry Smith } 1810efc6a03SBarry Smith 18268e69593SBarry Smith #if defined(PETSC_HAVE_UNISTD_H) 18368e69593SBarry Smith #include <unistd.h> 18468e69593SBarry Smith #endif 18568e69593SBarry Smith 1860efc6a03SBarry Smith #undef __FUNCT__ 1870efc6a03SBarry Smith #define __FUNCT__ "PetscGoogleDriveAuthorize" 1880efc6a03SBarry Smith /*@C 1890efc6a03SBarry Smith PetscGoogleDriveAuthorize - Get authorization and refresh token for accessing Google drive from PETSc 1900efc6a03SBarry Smith 1910efc6a03SBarry Smith Not collective, only the first process in MPI_Comm does anything 1920efc6a03SBarry Smith 1930efc6a03SBarry Smith Input Parameters: 1940efc6a03SBarry Smith + comm - the MPI communicator 1950efc6a03SBarry Smith - tokensize - size of the token arrays 1960efc6a03SBarry Smith 1970efc6a03SBarry Smith Output Parameters: 1980efc6a03SBarry Smith + access_token - can be used with PetscGoogleDriveUpload() for this one session 1990efc6a03SBarry Smith - refresh_token - can be used for ever to obtain new access_tokens with PetscGoogleDriveRefresh(), guard this like a password 2000efc6a03SBarry Smith it gives access to your Google Drive 2010efc6a03SBarry Smith 2020efc6a03SBarry Smith Notes: This call requires stdout and stdin access from process 0 on the MPI communicator 2030efc6a03SBarry Smith 204c245270aSBarry Smith You can run src/sys/webclient/examples/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 207*2b26979fSBarry Smith Level: intermediate 208*2b26979fSBarry Smith 2090efc6a03SBarry Smith .seealso: PetscGoogleDriveRefresh(), PetscGoogleDriveUpload(), PetscURLShorten() 2100efc6a03SBarry Smith 2110efc6a03SBarry Smith @*/ 2120efc6a03SBarry Smith PetscErrorCode PetscGoogleDriveAuthorize(MPI_Comm comm,char access_token[],char refresh_token[],size_t tokensize) 2130efc6a03SBarry Smith { 2140efc6a03SBarry Smith SSL_CTX *ctx; 2150efc6a03SBarry Smith SSL *ssl; 2160efc6a03SBarry Smith int sock; 2170efc6a03SBarry Smith PetscErrorCode ierr; 2185dc0f0a4SBarry Smith char buff[8*1024],*ptr,body[1024]; 2190efc6a03SBarry Smith PetscMPIInt rank; 2200efc6a03SBarry Smith size_t len; 2215dc0f0a4SBarry Smith PetscBool found; 2220efc6a03SBarry Smith 2230efc6a03SBarry Smith PetscFunctionBegin; 22468e69593SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 22568e69593SBarry Smith if (!rank) { 22668e69593SBarry Smith if (!isatty(fileno(PETSC_STDOUT))) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Requires users input/output"); 2274a285bdaSBarry Smith ierr = PetscPrintf(comm,"Cut and paste the following into your browser:\n\n" 2280efc6a03SBarry Smith "https://accounts.google.com/o/oauth2/auth?" 2290efc6a03SBarry Smith "scope=https%%3A%%2F%%2Fwww.googleapis.com%%2Fauth%%2Fdrive.file&" 2300efc6a03SBarry Smith "redirect_uri=urn:ietf:wg:oauth:2.0:oob&" 2310efc6a03SBarry Smith "response_type=code&" 2320efc6a03SBarry Smith "client_id=" 2330efc6a03SBarry Smith PETSC_GOOGLE_CLIENT_ID 2340efc6a03SBarry Smith "\n\n");CHKERRQ(ierr); 2350efc6a03SBarry Smith ierr = PetscPrintf(comm,"Paste the result here:");CHKERRQ(ierr); 2360efc6a03SBarry Smith ptr = fgets(buff, 1024, stdin); 2370efc6a03SBarry Smith if (!ptr) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_FILE_READ, "Error reading from stdin: %d", errno); 2380efc6a03SBarry Smith ierr = PetscStrlen(buff,&len);CHKERRQ(ierr); 2390efc6a03SBarry Smith buff[len-1] = 0; /* remove carriage return at end of line */ 2400efc6a03SBarry Smith 2410efc6a03SBarry Smith ierr = PetscSSLInitializeContext(&ctx);CHKERRQ(ierr); 2420efc6a03SBarry Smith ierr = PetscHTTPSConnect("accounts.google.com",443,ctx,&sock,&ssl);CHKERRQ(ierr); 2430efc6a03SBarry Smith ierr = PetscStrcpy(body,"code=");CHKERRQ(ierr); 2440efc6a03SBarry Smith ierr = PetscStrcat(body,buff);CHKERRQ(ierr); 2450efc6a03SBarry Smith ierr = PetscStrcat(body,"&client_id=");CHKERRQ(ierr); 2460efc6a03SBarry Smith ierr = PetscStrcat(body,PETSC_GOOGLE_CLIENT_ID);CHKERRQ(ierr); 2470efc6a03SBarry Smith ierr = PetscStrcat(body,"&client_secret=");CHKERRQ(ierr); 2480efc6a03SBarry Smith ierr = PetscStrcat(body,PETSC_GOOGLE_CLIENT_ST);CHKERRQ(ierr); 2490efc6a03SBarry Smith ierr = PetscStrcat(body,"&redirect_uri=urn:ietf:wg:oauth:2.0:oob&");CHKERRQ(ierr); 2500efc6a03SBarry Smith ierr = PetscStrcat(body,"grant_type=authorization_code");CHKERRQ(ierr); 2510efc6a03SBarry Smith 25293e1d32fSBarry Smith ierr = PetscHTTPSRequest("POST","accounts.google.com/o/oauth2/token",NULL,"application/x-www-form-urlencoded",body,ssl,buff,sizeof(buff));CHKERRQ(ierr); 2530efc6a03SBarry Smith ierr = PetscSSLDestroyContext(ctx);CHKERRQ(ierr); 2540efc6a03SBarry Smith close(sock); 2550efc6a03SBarry Smith 2565dc0f0a4SBarry Smith ierr = PetscPullJSONValue(buff,"access_token",access_token,tokensize,&found);CHKERRQ(ierr); 2575dc0f0a4SBarry Smith if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Google drive did not return access_token"); 2585dc0f0a4SBarry Smith ierr = PetscPullJSONValue(buff,"refresh_token",refresh_token,tokensize,&found);CHKERRQ(ierr); 2595dc0f0a4SBarry Smith if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Google drive did not return refresh_token"); 2600efc6a03SBarry Smith 2610efc6a03SBarry 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); 2625dc0f0a4SBarry Smith ierr = PetscPrintf(comm,"programs with the option -google_refresh_token %s\n",refresh_token);CHKERRQ(ierr); 2630efc6a03SBarry Smith ierr = PetscPrintf(comm,"to access Google Drive automatically\n");CHKERRQ(ierr); 2640efc6a03SBarry Smith } 2650efc6a03SBarry Smith PetscFunctionReturn(0); 2660efc6a03SBarry Smith } 2670efc6a03SBarry Smith 2680efc6a03SBarry Smith 2690efc6a03SBarry Smith #undef __FUNCT__ 2700efc6a03SBarry Smith #define __FUNCT__ "PetscURLShorten" 2710efc6a03SBarry Smith /*@C 2720efc6a03SBarry Smith PetscURLShorten - Uses Google's service to get a short url for a long url 2730efc6a03SBarry Smith 2740efc6a03SBarry Smith Input Parameters: 2750efc6a03SBarry Smith + url - long URL you want shortened 2760efc6a03SBarry Smith - lenshorturl - length of buffer to contain short URL 2770efc6a03SBarry Smith 2780efc6a03SBarry Smith Output Parameter: 2790efc6a03SBarry Smith . shorturl - the shortened URL 2800efc6a03SBarry Smith 281*2b26979fSBarry Smith Level: intermediate 282*2b26979fSBarry Smith 2830efc6a03SBarry Smith .seealso: PetscGoogleDriveRefresh(), PetscGoogleDriveUpload(), PetscGoogleDriveAuthorize() 2840efc6a03SBarry Smith @*/ 2850efc6a03SBarry Smith PetscErrorCode PetscURLShorten(const char url[],char shorturl[],size_t lenshorturl) 2860efc6a03SBarry Smith { 2870efc6a03SBarry Smith SSL_CTX *ctx; 2880efc6a03SBarry Smith SSL *ssl; 2890efc6a03SBarry Smith int sock; 2900efc6a03SBarry Smith PetscErrorCode ierr; 2915dc0f0a4SBarry Smith char buff[1024],body[512]; 2925dc0f0a4SBarry Smith PetscBool found; 2930efc6a03SBarry Smith 2940efc6a03SBarry Smith PetscFunctionBegin; 2950efc6a03SBarry Smith ierr = PetscSSLInitializeContext(&ctx);CHKERRQ(ierr); 2960efc6a03SBarry Smith ierr = PetscHTTPSConnect("www.googleapis.com",443,ctx,&sock,&ssl);CHKERRQ(ierr); 2975dc0f0a4SBarry Smith ierr = PetscStrcpy(body,"{");CHKERRQ(ierr); 2985dc0f0a4SBarry Smith ierr = PetscPushJSONValue(body,"longUrl",url,sizeof(body)-2);CHKERRQ(ierr); 2995dc0f0a4SBarry Smith ierr = PetscStrcat(body,"}");CHKERRQ(ierr); 30093e1d32fSBarry Smith ierr = PetscHTTPSRequest("POST","www.googleapis.com/urlshortener/v1/url",NULL,"application/json",body,ssl,buff,sizeof(buff));CHKERRQ(ierr); 3010efc6a03SBarry Smith ierr = PetscSSLDestroyContext(ctx);CHKERRQ(ierr); 3020efc6a03SBarry Smith close(sock); 3035dc0f0a4SBarry Smith 3045dc0f0a4SBarry Smith ierr = PetscPullJSONValue(buff,"id",shorturl,lenshorturl,&found);CHKERRQ(ierr); 3055dc0f0a4SBarry Smith if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Google drive did not return short URL"); 3060efc6a03SBarry Smith PetscFunctionReturn(0); 3070efc6a03SBarry Smith } 3080efc6a03SBarry Smith 309