在Java的Google驱动器中使用Google Spreadsheet API创建电子表格

在Java的Google驱动器中使用Google Spreadsheet API创建电子表格,第1张

在Java的Google驱动器中使用Google Spreadsheet API创建电子表格

我终于在这里的帮助下设法建立了这种联系。一切都像以前一样工作。您要做的步骤是:

  1. 在https://console.developers.google.com上注册
  2. 建立新专案
  3. 在“ API和身份验证”下->“凭据”->“为服务帐户创建新的客户端ID”
  4. 生成客户端ID时,必须生成P12密钥。
  5. 下面的代码将需要客户ID,电子邮件地址是您必须共享电子表格的地址

下面是工作代码

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;import com.google.api.client.http.HttpTransport;import com.google.api.client.http.javanet.NetHttpTransport;import com.google.api.client.json.jackson.JacksonFactory;import com.google.gdata.client.spreadsheet.SpreadsheetService;import com.google.gdata.data.spreadsheet.SpreadsheetEntry;import com.google.gdata.data.spreadsheet.SpreadsheetFeed;import com.google.gdata.util.ServiceException;import java.io.File;import java.io.IOException;import java.net.MalformedURLException;import java.net.URL;import java.security.GeneralSecurityException;import java.util.Arrays;import java.util.List;public class OAuthIntegration{    public static void main(String[] args) throws MalformedURLException, GeneralSecurityException, IOException, ServiceException {        URL SPREADSHEET_FEED_URL;        SPREADSHEET_FEED_URL = new URL("https://spreadsheets.google.com/feeds/spreadsheets/private/full");        File p12 = new File("./key.p12");        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 SCOPES = Arrays.asList(SCOPESArray);        GoogleCredential credential = new GoogleCredential.Builder()     .setTransport(httpTransport)     .setJsonFactory(jsonFactory)     .setServiceAccountId("cliend_ID")     .setServiceAccountScopes(SCOPES)     .setServiceAccountPrivateKeyFromP12File(p12)     .build();        SpreadsheetService service = new SpreadsheetService("Test");        service.setOAuth2Credentials(credential);        SpreadsheetFeed feed = service.getFeed(SPREADSHEET_FEED_URL, SpreadsheetFeed.class);        List<SpreadsheetEntry> spreadsheets = feed.getEntries();        if (spreadsheets.size() == 0) { System.out.println("No spreadsheets found.");        }         SpreadsheetEntry spreadsheet = null;        for (int i = 0; i < spreadsheets.size(); i++) { if (spreadsheets.get(i).getTitle().getPlainText().startsWith("ListOfSandboxes")) {     spreadsheet = spreadsheets.get(i);     System.out.println("Name of editing spreadsheet: " + spreadsheets.get(i).getTitle().getPlainText());     System.out.println("ID of SpreadSheet: " + i); }        }    }}

我希望这将有助于解决我遇到的问题。这是我使用过的jar的列表:

guava-11.0.2.jargdata-spreadsheet-3.0.jargdata-maps-2.0.jargdata-core-1.0.jarjackson-core-asl-1.9.11.jarjackson-core-2.1.3.jargoogle-oauth-client-1.20.0.jargoogle-http-client-jackson2-1.20.0.jargoogle-http-client-jackson-1.20.0.jargoogle-http-client-1.20.0.jargoogle-api-client-1.20.0.jar


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5054299.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-15
下一篇 2022-11-16

发表评论

登录后才能评论

评论列表(0条)

保存