0

我想获取 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);
        }        
    }
4

1 回答 1

2

在您的代码中,更改

.setAccessType("online")

.setAccessType("offline")
于 2013-11-09T14:55:03.680 回答