int main(int argc,char **argv) { PetscErrorCode ierr; char access_token[512],refresh_token[512]; ierr = PetscInitialize(&argc,&argv,NULL,NULL);if (ierr) return ierr; ierr = PetscGoogleDriveAuthorize(PETSC_COMM_WORLD,access_token,refresh_token,sizeof(access_token));CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD,"Your Refresh token is %s\n",refresh_token);CHKERRQ(ierr); ierr = PetscFinalize(); return ierr; }
/*@C PetscGoogleDriveRefresh - Get a new authorization token for accessing Google drive from PETSc from a refresh token Not collective, only the first process in the MPI_Comm does anything Input Parameters: + comm - MPI communicator . refresh token - obtained with PetscGoogleDriveAuthorize(), if NULL PETSc will first look for one in the options data if not found it will call PetscGoogleDriveAuthorize() - tokensize - size of the output string access_token Output Parameter: . access_token - token that can be passed to PetscGoogleDriveUpload() Options Database: . -google_refresh_token XXX where XXX was obtained from PetscGoogleDriveAuthorize() Level: intermediate .seealso: PetscURLShorten(), PetscGoogleDriveAuthorize(), PetscGoogleDriveUpload() @*/ PetscErrorCode PetscGoogleDriveRefresh(MPI_Comm comm,const char refresh_token[],char access_token[],size_t tokensize) { SSL_CTX *ctx; SSL *ssl; int sock; PetscErrorCode ierr; char buff[8*1024],body[1024]; PetscMPIInt rank; char *refreshtoken = (char*)refresh_token; PetscBool found; PetscFunctionBegin; ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); if (!rank) { if (!refresh_token) { PetscBool set; ierr = PetscMalloc1(512,&refreshtoken);CHKERRQ(ierr); ierr = PetscOptionsGetString(NULL,NULL,"-google_refresh_token",refreshtoken,512,&set);CHKERRQ(ierr); if (!set) { ierr = PetscGoogleDriveAuthorize(comm,access_token,refreshtoken,512*sizeof(char));CHKERRQ(ierr); ierr = PetscFree(refreshtoken);CHKERRQ(ierr); PetscFunctionReturn(0); } } ierr = PetscSSLInitializeContext(&ctx);CHKERRQ(ierr); ierr = PetscHTTPSConnect("accounts.google.com",443,ctx,&sock,&ssl);CHKERRQ(ierr); ierr = PetscStrcpy(body,"client_id=");CHKERRQ(ierr); ierr = PetscStrcat(body,PETSC_GOOGLE_CLIENT_ID);CHKERRQ(ierr); ierr = PetscStrcat(body,"&client_secret=");CHKERRQ(ierr); ierr = PetscStrcat(body,PETSC_GOOGLE_CLIENT_ST);CHKERRQ(ierr); ierr = PetscStrcat(body,"&refresh_token=");CHKERRQ(ierr); ierr = PetscStrcat(body,refreshtoken);CHKERRQ(ierr); if (!refresh_token) {ierr = PetscFree(refreshtoken);CHKERRQ(ierr);} ierr = PetscStrcat(body,"&grant_type=refresh_token");CHKERRQ(ierr); ierr = PetscHTTPSRequest("POST","accounts.google.com/o/oauth2/token",NULL,"application/x-www-form-urlencoded",body,ssl,buff,sizeof(buff));CHKERRQ(ierr); ierr = PetscSSLDestroyContext(ctx);CHKERRQ(ierr); close(sock); ierr = PetscPullJSONValue(buff,"access_token",access_token,tokensize,&found);CHKERRQ(ierr); if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Google drive did not return access_token"); } PetscFunctionReturn(0); }