我如何获得refreshToken?

时间:2013-11-09 14:26:06

标签: java google-drive-api google-oauth google-oauth-java-client

我想获得refreshToken登录离线。 我已经获得了访问令牌,但我没有得到refreshToken。 这是我的源代码:

     public void OpenBrower(){
        httpTransport = new NetHttpTransport();
        jsonFactory = new JacksonFactory();              
        flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, jsonFactory, CLIENT_ID, CLIENT_SECRET, 
                Arrays.asList(DriveScopes.DRIVE)).setAccessType("online").setApprovalPrompt("auto").build();        
        String url = flow.newAuthorizationUrl().setRedirectUri(REDIRECT_URI).build();        
        try {
            java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));
        } catch (IOException ex) {
            Logger.getLogger(GoogleD.class.getName()).log(Level.SEVERE, null, ex);
        }        
    }
    public void Register(String code){
        GoogleTokenResponse response = null;
        try {
            response = flow.newTokenRequest(code).setRedirectUri(REDIRECT_URI).execute();
        } catch (IOException ex) {
            Logger.getLogger(GoogleD.class.getName()).log(Level.SEVERE, null, ex);
        }
        GoogleCredential credential = new GoogleCredential().setFromTokenResponse(response);

        client = new Drive.Builder(httpTransport, jsonFactory, credential).build();
        try {
            System.out.println(credential.refreshToken());
        } catch (IOException ex) {
            Logger.getLogger(GoogleD.class.getName()).log(Level.SEVERE, null, ex);
        }        
    }

1 个答案:

答案 0 :(得分:2)

在您的代码中,更改

.setAccessType("online")

.setAccessType("offline")