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 9*a8d69d7bSBarry 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 180efc6a03SBarry Smith /*@C 190efc6a03SBarry Smith PetscGoogleDriveRefresh - Get a new authorization token for accessing Google drive from PETSc from a refresh token 200efc6a03SBarry Smith 210efc6a03SBarry Smith Not collective, only the first process in the MPI_Comm does anything 220efc6a03SBarry Smith 230efc6a03SBarry Smith Input Parameters: 240efc6a03SBarry Smith + comm - MPI communicator 250efc6a03SBarry Smith . refresh token - obtained with PetscGoogleDriveAuthorize(), if NULL PETSc will first look for one in the options data 260efc6a03SBarry Smith if not found it will call PetscGoogleDriveAuthorize() 270efc6a03SBarry Smith - tokensize - size of the output string access_token 280efc6a03SBarry Smith 290efc6a03SBarry Smith Output Parameter: 300efc6a03SBarry Smith . access_token - token that can be passed to PetscGoogleDriveUpload() 310efc6a03SBarry Smith 324a285bdaSBarry Smith Options Database: 334a285bdaSBarry Smith . -google_refresh_token XXX where XXX was obtained from PetscGoogleDriveAuthorize() 344a285bdaSBarry Smith 352b26979fSBarry Smith Level: intermediate 364a285bdaSBarry Smith 370efc6a03SBarry Smith .seealso: PetscURLShorten(), PetscGoogleDriveAuthorize(), PetscGoogleDriveUpload() 380efc6a03SBarry Smith 390efc6a03SBarry Smith @*/ 400efc6a03SBarry Smith PetscErrorCode PetscGoogleDriveRefresh(MPI_Comm comm,const char refresh_token[],char access_token[],size_t tokensize) 410efc6a03SBarry Smith { 420efc6a03SBarry Smith SSL_CTX *ctx; 430efc6a03SBarry Smith SSL *ssl; 440efc6a03SBarry Smith int sock; 450efc6a03SBarry Smith PetscErrorCode ierr; 465dc0f0a4SBarry Smith char buff[8*1024],body[1024]; 470efc6a03SBarry Smith PetscMPIInt rank; 480efc6a03SBarry Smith char *refreshtoken = (char*)refresh_token; 495dc0f0a4SBarry Smith PetscBool found; 500efc6a03SBarry Smith 510efc6a03SBarry Smith PetscFunctionBegin; 520efc6a03SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 530efc6a03SBarry Smith if (!rank) { 540efc6a03SBarry Smith if (!refresh_token) { 550efc6a03SBarry Smith PetscBool set; 560efc6a03SBarry Smith ierr = PetscMalloc1(512,&refreshtoken);CHKERRQ(ierr); 57c5929fdfSBarry Smith ierr = PetscOptionsGetString(NULL,NULL,"-google_refresh_token",refreshtoken,512,&set);CHKERRQ(ierr); 580efc6a03SBarry Smith if (!set) { 590efc6a03SBarry Smith ierr = PetscGoogleDriveAuthorize(comm,access_token,refreshtoken,512*sizeof(char));CHKERRQ(ierr); 600efc6a03SBarry Smith ierr = PetscFree(refreshtoken);CHKERRQ(ierr); 610efc6a03SBarry Smith PetscFunctionReturn(0); 620efc6a03SBarry Smith } 630efc6a03SBarry Smith } 640efc6a03SBarry Smith ierr = PetscSSLInitializeContext(&ctx);CHKERRQ(ierr); 650efc6a03SBarry Smith ierr = PetscHTTPSConnect("accounts.google.com",443,ctx,&sock,&ssl);CHKERRQ(ierr); 664a285bdaSBarry Smith ierr = PetscStrcpy(body,"client_id=");CHKERRQ(ierr); 670efc6a03SBarry Smith ierr = PetscStrcat(body,PETSC_GOOGLE_CLIENT_ID);CHKERRQ(ierr); 680efc6a03SBarry Smith ierr = PetscStrcat(body,"&client_secret=");CHKERRQ(ierr); 690efc6a03SBarry Smith ierr = PetscStrcat(body,PETSC_GOOGLE_CLIENT_ST);CHKERRQ(ierr); 700efc6a03SBarry Smith ierr = PetscStrcat(body,"&refresh_token=");CHKERRQ(ierr); 710efc6a03SBarry Smith ierr = PetscStrcat(body,refreshtoken);CHKERRQ(ierr); 720efc6a03SBarry Smith if (!refresh_token) {ierr = PetscFree(refreshtoken);CHKERRQ(ierr);} 730efc6a03SBarry Smith ierr = PetscStrcat(body,"&grant_type=refresh_token");CHKERRQ(ierr); 740efc6a03SBarry Smith 7593e1d32fSBarry Smith ierr = PetscHTTPSRequest("POST","accounts.google.com/o/oauth2/token",NULL,"application/x-www-form-urlencoded",body,ssl,buff,sizeof(buff));CHKERRQ(ierr); 760efc6a03SBarry Smith ierr = PetscSSLDestroyContext(ctx);CHKERRQ(ierr); 770efc6a03SBarry Smith close(sock); 780efc6a03SBarry Smith 795dc0f0a4SBarry Smith ierr = PetscPullJSONValue(buff,"access_token",access_token,tokensize,&found);CHKERRQ(ierr); 805dc0f0a4SBarry Smith if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Google drive did not return access_token"); 810efc6a03SBarry Smith } 820efc6a03SBarry Smith PetscFunctionReturn(0); 830efc6a03SBarry Smith } 840efc6a03SBarry Smith 850efc6a03SBarry Smith #include <sys/stat.h> 860efc6a03SBarry Smith 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 1054683183fSBarry Smith PetscGoogleDriveUpload(comm,NULL,filename); for first use will prompt user to authorize access to Google Drive with their browser 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 1182b26979fSBarry Smith Level: intermediate 1192b26979fSBarry Smith 1200efc6a03SBarry Smith .seealso: PetscURLShorten(), PetscGoogleDriveAuthorize(), PetscGoogleDriveRefresh() 1210efc6a03SBarry Smith 1220efc6a03SBarry Smith @*/ 1230efc6a03SBarry Smith PetscErrorCode PetscGoogleDriveUpload(MPI_Comm comm,const char access_token[],const char filename[]) 1240efc6a03SBarry Smith { 1250efc6a03SBarry Smith SSL_CTX *ctx; 1260efc6a03SBarry Smith SSL *ssl; 1270efc6a03SBarry Smith int sock; 1280efc6a03SBarry Smith PetscErrorCode ierr; 1290efc6a03SBarry Smith char head[1024],buff[8*1024],*body,*title; 1300efc6a03SBarry Smith PetscMPIInt rank; 1310efc6a03SBarry Smith struct stat sb; 1320efc6a03SBarry Smith size_t len,blen,rd; 1330efc6a03SBarry Smith FILE *fd; 1340efc6a03SBarry Smith 1350efc6a03SBarry Smith PetscFunctionBegin; 1360efc6a03SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 1370efc6a03SBarry Smith if (!rank) { 1380efc6a03SBarry Smith ierr = PetscStrcpy(head,"Authorization: Bearer ");CHKERRQ(ierr); 1390efc6a03SBarry Smith ierr = PetscStrcat(head,access_token);CHKERRQ(ierr); 1400efc6a03SBarry Smith ierr = PetscStrcat(head,"\r\n");CHKERRQ(ierr); 1410efc6a03SBarry Smith ierr = PetscStrcat(head,"uploadType: multipart\r\n");CHKERRQ(ierr); 1420efc6a03SBarry Smith 1430efc6a03SBarry Smith ierr = stat(filename,&sb); 1440efc6a03SBarry Smith if (ierr) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to stat file: %s",filename); 1450efc6a03SBarry Smith len = 1024 + sb.st_size; 1460efc6a03SBarry Smith ierr = PetscMalloc1(len,&body);CHKERRQ(ierr); 1470efc6a03SBarry Smith ierr = PetscStrcpy(body,"--foo_bar_baz\r\n" 1480efc6a03SBarry Smith "Content-Type: application/json\r\n\r\n" 1495dc0f0a4SBarry Smith "{");CHKERRQ(ierr); 1505dc0f0a4SBarry Smith ierr = PetscPushJSONValue(body,"title",filename,len);CHKERRQ(ierr); 1515dc0f0a4SBarry Smith ierr = PetscStrcat(body,",");CHKERRQ(ierr); 1525dc0f0a4SBarry Smith ierr = PetscPushJSONValue(body,"mimeType","text.html",len);CHKERRQ(ierr); 1535dc0f0a4SBarry Smith ierr = PetscStrcat(body,",");CHKERRQ(ierr); 1545dc0f0a4SBarry Smith ierr = PetscPushJSONValue(body,"description","a file",len);CHKERRQ(ierr); 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); 1580efc6a03SBarry Smith ierr = PetscStrlen(body,&blen);CHKERRQ(ierr); 1590efc6a03SBarry Smith fd = fopen (filename, "r"); 1600efc6a03SBarry Smith if (!fd) SETERRQ1(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); 162d8dcb26dSBarry 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); 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); 1670efc6a03SBarry Smith ierr = PetscSSLInitializeContext(&ctx);CHKERRQ(ierr); 1680efc6a03SBarry Smith ierr = PetscHTTPSConnect("www.googleapis.com",443,ctx,&sock,&ssl);CHKERRQ(ierr); 16993e1d32fSBarry 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); 1700efc6a03SBarry Smith ierr = PetscFree(body);CHKERRQ(ierr); 1710efc6a03SBarry Smith ierr = PetscSSLDestroyContext(ctx);CHKERRQ(ierr); 1720efc6a03SBarry Smith close(sock); 1730efc6a03SBarry Smith ierr = PetscStrstr(buff,"\"title\"",&title);CHKERRQ(ierr); 1740efc6a03SBarry Smith if (!title) SETERRQ1(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 200c245270aSBarry Smith You can run src/sys/webclient/examples/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; 22068e69593SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 22168e69593SBarry Smith if (!rank) { 22268e69593SBarry Smith if (!isatty(fileno(PETSC_STDOUT))) SETERRQ(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); 2310efc6a03SBarry Smith ierr = PetscPrintf(comm,"Paste the result here:");CHKERRQ(ierr); 2320efc6a03SBarry Smith ptr = fgets(buff, 1024, stdin); 2330efc6a03SBarry Smith if (!ptr) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_FILE_READ, "Error reading from stdin: %d", errno); 2340efc6a03SBarry Smith ierr = PetscStrlen(buff,&len);CHKERRQ(ierr); 2350efc6a03SBarry Smith buff[len-1] = 0; /* remove carriage return at end of line */ 2360efc6a03SBarry Smith 2370efc6a03SBarry Smith ierr = PetscSSLInitializeContext(&ctx);CHKERRQ(ierr); 2380efc6a03SBarry Smith ierr = PetscHTTPSConnect("accounts.google.com",443,ctx,&sock,&ssl);CHKERRQ(ierr); 2390efc6a03SBarry Smith ierr = PetscStrcpy(body,"code=");CHKERRQ(ierr); 2400efc6a03SBarry Smith ierr = PetscStrcat(body,buff);CHKERRQ(ierr); 2410efc6a03SBarry Smith ierr = PetscStrcat(body,"&client_id=");CHKERRQ(ierr); 2420efc6a03SBarry Smith ierr = PetscStrcat(body,PETSC_GOOGLE_CLIENT_ID);CHKERRQ(ierr); 2430efc6a03SBarry Smith ierr = PetscStrcat(body,"&client_secret=");CHKERRQ(ierr); 2440efc6a03SBarry Smith ierr = PetscStrcat(body,PETSC_GOOGLE_CLIENT_ST);CHKERRQ(ierr); 2450efc6a03SBarry Smith ierr = PetscStrcat(body,"&redirect_uri=urn:ietf:wg:oauth:2.0:oob&");CHKERRQ(ierr); 2460efc6a03SBarry Smith ierr = PetscStrcat(body,"grant_type=authorization_code");CHKERRQ(ierr); 2470efc6a03SBarry Smith 24893e1d32fSBarry Smith ierr = PetscHTTPSRequest("POST","accounts.google.com/o/oauth2/token",NULL,"application/x-www-form-urlencoded",body,ssl,buff,sizeof(buff));CHKERRQ(ierr); 2490efc6a03SBarry Smith ierr = PetscSSLDestroyContext(ctx);CHKERRQ(ierr); 2500efc6a03SBarry Smith close(sock); 2510efc6a03SBarry Smith 2525dc0f0a4SBarry Smith ierr = PetscPullJSONValue(buff,"access_token",access_token,tokensize,&found);CHKERRQ(ierr); 2535dc0f0a4SBarry Smith if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Google drive did not return access_token"); 2545dc0f0a4SBarry Smith ierr = PetscPullJSONValue(buff,"refresh_token",refresh_token,tokensize,&found);CHKERRQ(ierr); 2555dc0f0a4SBarry Smith if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Google drive did not return refresh_token"); 2560efc6a03SBarry Smith 2570efc6a03SBarry 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); 2585dc0f0a4SBarry Smith ierr = PetscPrintf(comm,"programs with the option -google_refresh_token %s\n",refresh_token);CHKERRQ(ierr); 2590efc6a03SBarry Smith ierr = PetscPrintf(comm,"to access Google Drive automatically\n");CHKERRQ(ierr); 2600efc6a03SBarry Smith } 2610efc6a03SBarry Smith PetscFunctionReturn(0); 2620efc6a03SBarry Smith } 2630efc6a03SBarry Smith 2640efc6a03SBarry Smith 2650efc6a03SBarry Smith /*@C 2660efc6a03SBarry Smith PetscURLShorten - Uses Google's service to get a short url for a long url 2670efc6a03SBarry Smith 2680efc6a03SBarry Smith Input Parameters: 2690efc6a03SBarry Smith + url - long URL you want shortened 2700efc6a03SBarry Smith - lenshorturl - length of buffer to contain short URL 2710efc6a03SBarry Smith 2720efc6a03SBarry Smith Output Parameter: 2730efc6a03SBarry Smith . shorturl - the shortened URL 2740efc6a03SBarry Smith 2752b26979fSBarry Smith Level: intermediate 2762b26979fSBarry Smith 2770efc6a03SBarry Smith .seealso: PetscGoogleDriveRefresh(), PetscGoogleDriveUpload(), PetscGoogleDriveAuthorize() 2780efc6a03SBarry Smith @*/ 2790efc6a03SBarry Smith PetscErrorCode PetscURLShorten(const char url[],char shorturl[],size_t lenshorturl) 2800efc6a03SBarry Smith { 2810efc6a03SBarry Smith SSL_CTX *ctx; 2820efc6a03SBarry Smith SSL *ssl; 2830efc6a03SBarry Smith int sock; 2840efc6a03SBarry Smith PetscErrorCode ierr; 2855708bc22SBarry Smith char buff[1024],body[512],post[1024]; 2865dc0f0a4SBarry Smith PetscBool found; 2870efc6a03SBarry Smith 2880efc6a03SBarry Smith PetscFunctionBegin; 2890efc6a03SBarry Smith ierr = PetscSSLInitializeContext(&ctx);CHKERRQ(ierr); 2900efc6a03SBarry Smith ierr = PetscHTTPSConnect("www.googleapis.com",443,ctx,&sock,&ssl);CHKERRQ(ierr); 2915dc0f0a4SBarry Smith ierr = PetscStrcpy(body,"{");CHKERRQ(ierr); 2925dc0f0a4SBarry Smith ierr = PetscPushJSONValue(body,"longUrl",url,sizeof(body)-2);CHKERRQ(ierr); 2935dc0f0a4SBarry Smith ierr = PetscStrcat(body,"}");CHKERRQ(ierr); 2945708bc22SBarry Smith ierr = PetscSNPrintf(post,sizeof(post),"www.googleapis.com/urlshortener/v1/url?key=%s",PETSC_GOOGLE_API_KEY);CHKERRQ(ierr); 2955708bc22SBarry Smith ierr = PetscHTTPSRequest("POST",post,NULL,"application/json",body,ssl,buff,sizeof(buff));CHKERRQ(ierr); 2960efc6a03SBarry Smith ierr = PetscSSLDestroyContext(ctx);CHKERRQ(ierr); 2970efc6a03SBarry Smith close(sock); 2985dc0f0a4SBarry Smith 2995dc0f0a4SBarry Smith ierr = PetscPullJSONValue(buff,"id",shorturl,lenshorturl,&found);CHKERRQ(ierr); 3005dc0f0a4SBarry Smith if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Google drive did not return short URL"); 3010efc6a03SBarry Smith PetscFunctionReturn(0); 3020efc6a03SBarry Smith } 3030efc6a03SBarry Smith 304