【问题标题】:How do I get refreshToken?如何获得 refreshToken?
【发布时间】:2013-11-21 12:38:31
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    在你的代码中,改变

    .setAccessType("online")
    

    .setAccessType("offline")
    

    【讨论】:

    • 谢谢你..你帮助了我很多次...我有一些问题。所以..我会问,我想让你对我的问题感兴趣。
    • 你能检查一下这个问题吗?...stackoverflow.com/questions/19877831/…
    • 对不起 - 我不知道。我不喜欢使用 Oauth 库,所以我使用原始的 http 请求,因为我发现它们更容易调试,而且实现起来也不难。
    猜你喜欢
    • 2022-07-22
    • 2021-09-08
    • 2021-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    • 2021-03-21
    相关资源
    最近更新 更多