简体   繁体   English

Java Google Sheets API OAuth 2.0

[英]Java Google Sheets API OAuth 2.0

I'm having problems setting up my program to make calls to the Google Sheets API. 我在设置程序以调用Google Sheets API时遇到问题。 Currently, I'm authorizing my credential this way: 目前,我正在以这种方式授权我的证书:

public static Credential authorize() throws IOException, GeneralSecurityException {
    InputStream p12 = GoogleUtils.class.getResourceAsStream("/key.p12");

    File file = new File("hi");
    OutputStream outputStream = new FileOutputStream(file);
    IOUtils.copy(p12, outputStream);
    outputStream.close();

    // Build flow and trigger user authorization request.
   GoogleCredential credential = new GoogleCredential.Builder()
        .setTransport(HTTP_TRANSPORT)
        .setJsonFactory(JSON_FACTORY)
        .setServiceAccountId("8429209348-1nfuorcfko5pmqh2l0b1au968igchaoq@developer.gserviceaccount.com")
        .setServiceAccountScopes(SCOPES)
        .setServiceAccountPrivateKeyFromP12File(file)
        .build();
    return credential;
}

But, I don't know what to do with this credential now that I have it. 但是,现在我有了证书,我不知道该怎么办。 In another class, I'm opening up a SpreadsheetService , but I can't find the method to actually authorize with the Credential object. 在另一个类中,我打开了SpreadsheetService ,但是找不到使用Credential对象进行实际授权的方法。 I remember doing this in one of my older projects: 我记得在我的一个较早的项目中这样做:

public GoogleDrive() {
    /** Our view of Google Spreadsheets as an authenticated Google user. */
    try {
        InputStream p12 = BotPanel.class.getResourceAsStream("/key.p12");

        File file = new File("hi");
        OutputStream outputStream = new FileOutputStream(file);
        IOUtils.copy(p12, outputStream);
        outputStream.close();

        String emailAddress = "81607255786-225dt8i2s8q6qgi0lgg814p27mumqro2@developer.gserviceaccount.com";
        HttpTransport httpTransport = new NetHttpTransport();
        JacksonFactory jsonFactory = new JacksonFactory();
        String[] SCOPESArray = { "https://spreadsheets.google.com/feeds", "https://spreadsheets.google.com/feeds/spreadsheets/private/full", "https://docs.google.com/feeds" };
        final List<String> SCOPES = Arrays.asList(SCOPESArray);
        GoogleCredential credential = new GoogleCredential.Builder()
                    .setTransport(httpTransport).setJsonFactory(jsonFactory)
                    .setServiceAccountId(emailAddress).setServiceAccountScopes(SCOPES)
                    .setServiceAccountPrivateKeyFromP12File(file)
                    .build();

        file.delete();
        service = new SpreadsheetService("Test");
        service.setOAuth2Credentials(credential);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

As you can see I get the Credential the same way, and at the end I'm able to just call service.setOAuth2Credentials(credential); 如您所见,我以相同的方式获取Credential ,最后我可以仅调用service.setOAuth2Credentials(credential); But in my newer project, this is what I'm getting: 但是在我的新项目中,这就是我得到的:

在此处输入图片说明

As you can see there isn't a method called .setOAuth2Credential(...) anymore. 如您所见,不再有一种名为.setOAuth2Credential(...)的方法。

Digging into the src code, I found the method in the GoogleService class, only it had the annotation @Beta. 深入研究src代码,我在GoogleService类中找到了该方法,只有该方法具有注释@Beta。

@Beta
  public void setOAuth2Credentials(Credential credential) {
    GoogleAuthTokenFactory googleAuthTokenFactory = getGoogleAuthTokenFactory();
    googleAuthTokenFactory.setOAuth2Credentials(credential);
    requestFactory.setAuthToken(authTokenFactory.getAuthToken());
  }

Is there a reason why this code is annotated with @Beta? 用@Beta注释此代码是否有原因? Is there some other way I'm supposed to authorize calls to the sheets api? 我还有其他方法可以授权对表格api的调用吗? Or is there some way to still authorize with this method? 还是有一些方法仍可以使用此方法进行授权? (that would be ideal). (那将是理想的)。

The answer was simple. 答案很简单。 I had the wrong dependencies the whole time. 我一直有错误的依赖关系。 Make sure that you have gdata-client version 1.47. 确保您具有gdata-client版本1.47。 Here's a link to the jar file http://mvnrepository.com/artifact/com.google.gdata/core/1.47.1 . 以下是指向jar文件http://mvnrepository.com/artifact/com.google.gdata/core/1.47.1的链接。 Once I used that as a library all my problems went away :) 一旦我将其用作图书馆,我的所有问题都会消失:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM