int main(int argc,char **argv) { PetscErrorCode ierr; char access_token[512],refresh_token[512]; PetscInitialize(&argc,&argv,NULL,NULL); ierr = PetscBoxAuthorize(PETSC_COMM_WORLD,access_token,refresh_token,sizeof(access_token));CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD,"Your one time refresh token is %s\n",refresh_token);CHKERRQ(ierr); PetscFinalize(); return 0; }
/*@C PetscBoxRefresh - Get a new authorization token for accessing Box 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 PetscBoxAuthorize(), if NULL PETSc will first look for one in the options data if not found it will call PetscBoxAuthorize() - tokensize - size of the output string access_token Output Parameter: + access_token - token that can be passed to PetscBoxUpload() - new_refresh_token - the old refresh token is no longer valid, not this is different than Google where the same refresh_token is used forever Level: intermediate .seealso: PetscURLShorten(), PetscBoxAuthorize(), PetscBoxUpload() @*/ PetscErrorCode PetscBoxRefresh(MPI_Comm comm,const char refresh_token[],char access_token[],char new_refresh_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,"-box_refresh_token",refreshtoken,512,&set);CHKERRQ(ierr); #if defined(PETSC_HAVE_SAWS) if (!set) { ierr = PetscBoxAuthorize(comm,access_token,new_refresh_token,512*sizeof(char));CHKERRQ(ierr); ierr = PetscFree(refreshtoken);CHKERRQ(ierr); PetscFunctionReturn(0); } #else if (!set) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Must provide refresh token with -box_refresh_token XXX"); #endif } ierr = PetscSSLInitializeContext(&ctx);CHKERRQ(ierr); ierr = PetscHTTPSConnect("www.box.com",443,ctx,&sock,&ssl);CHKERRQ(ierr); ierr = PetscStrcpy(body,"client_id=");CHKERRQ(ierr); ierr = PetscStrcat(body,PETSC_BOX_CLIENT_ID);CHKERRQ(ierr); ierr = PetscStrcat(body,"&client_secret=");CHKERRQ(ierr); ierr = PetscStrcat(body,PETSC_BOX_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","www.box.com/api/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,"Box did not return access token"); ierr = PetscPullJSONValue(buff,"refresh_token",new_refresh_token,tokensize,&found);CHKERRQ(ierr); if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Box did not return refresh token"); ierr = PetscPrintf(comm,"Here is your new Box refresh token, save it in a save place, in the future you can run PETSc\n");CHKERRQ(ierr); ierr = PetscPrintf(comm,"programs with the option -box_refresh_token %s\n",new_refresh_token);CHKERRQ(ierr); ierr = PetscPrintf(comm,"to access Box Drive automatically\n");CHKERRQ(ierr); } PetscFunctionReturn(0); }