首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >谷歌OAuth2 Java代码每次都询问权限

谷歌OAuth2 Java代码每次都询问权限
EN

Stack Overflow用户
提问于 2020-07-22 22:07:14
回答 1查看 49关注 0票数 0

我将以下代码与google-client-secret.json文件一起使用,并尝试将其作为eclipse中的java应用程序运行。我想存储权限,这样一旦我接受了权限,它就不会再询问了。现在,它每次都在提示。在那之后,一切都像预期的那样工作,并写入我的google工作表。

代码语言:javascript
复制
    public static Credential authorizeSHEETS() throws IOException, GeneralSecurityException {
        File fileIn = new File("src/jg/sos/orders/google-sheets-client-secret.json");
//        InputStream in = GoogleAuthorizeUtil.class.getResourceAsStream("src/jg/sos/orders/google-sheets-client-secret.json");
        InputStream in = new FileInputStream(fileIn);
        GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JacksonFactory.getDefaultInstance(), new InputStreamReader(in));

        List<String> scopes = Arrays.asList(SheetsScopes.SPREADSHEETS);

        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), clientSecrets, scopes).setDataStoreFactory(new MemoryDataStoreFactory())
                .setAccessType("offline").setApprovalPrompt("auto").build();
        Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");

        return credential;
    }

有没有什么想法可以让它只在第一次提示我权限,然后下一次我运行它就不会了?

谢谢你的帮助!JJ

EN

回答 1

Stack Overflow用户

发布于 2020-07-23 02:38:18

所以我找到了这个问题的答案,以防有人遇到这个问题。我改为使用服务帐户,并为其下载json文件并将其放入我的项目中。

然后我只是引用了它,并使用DataStoreFactory保存了令牌,如下所示:

代码语言:javascript
复制
public static Credential authorizeSHEETS() throws IOException, GeneralSecurityException {
            File fileIn = new File("src/jg/sos/orders/google-sheets-client-secret.json");
    //        InputStream in = GoogleAuthorizeUtil.class.getResourceAsStream("src/jg/sos/orders/google-sheets-client-secret.json");
            InputStream in = new FileInputStream(fileIn);
            GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JacksonFactory.getDefaultInstance(), new InputStreamReader(in));
    
            List<String> scopes = Arrays.asList(SheetsScopes.SPREADSHEETS);
            
            FileDataStoreFactory dataStoreFactory = new FileDataStoreFactory(new File("src/jg/sos/orders"));
    
            GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), clientSecrets, scopes)
                    .setDataStoreFactory(dataStoreFactory)
                    .setAccessType("offline").setApprovalPrompt("auto").build();
            
            Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
            
            System.out.println("token" + credential.getAccessToken());
    
            return credential;
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63036180

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档