获取访问谷歌电子表格(保存到谷歌驱动器帐户)从java谷歌应用程序引擎(servlet)



我真的卡在这里了。我想从谷歌应用程序引擎访问一个谷歌电子表格(与java,使用servlet,因为这个想法是从电子表格中读取信息,保存在我的谷歌驱动器帐户,并通过"jsp"显示有限的用户数量,在我的公司)。

当然,我已经在"google cloud developer console"中创建了我的项目,我得到了"project id"第四case- xxxx等等。直到这一步,我才明白了一切,一切都很顺利。

我一直在这里寻找成千上万的例子,在谷歌等。对于我读到的一切,我都明白:我必须创建一个"OAuth2凭据"(为什么我在我的开发控制台的"Api和身份验证"中创建OAuth2凭据的原因(我得到了一个带有auth_uri, client_secret, client_id等的json)。

从我读到的只是需要client_secret和client_id。但是在这个教程(链接)之后,我得到了这个错误"oauth_token不存在"。在其他教程中,我读到没有必要在应用引擎中使用"OAuth2"。我很晕。

我唯一想要的是一个简单的java servlet(没什么复杂的,遵循良好的实践,我不在乎),从谷歌电子表格读取数据(保存到我的谷歌驱动器帐户)并通过"jsp"显示,至少我满足于接受我的servlet上接收电子表格数据,稍后我将想象如何显示

我使用eclipse luna,我安装了"gdata java api","google app engine as localhost"等(全部正确安装,运行无错误)。按照本教程

创建环境

的一些问题:电子表格需要发布吗?(菜单:file->publish to web)。从应用程序引擎获得访问权限?2-测试我的代码(看看我是否有权访问电子表格)在强制上传到谷歌应用程序引擎(http://.appspot.com/guestbook),或者我可以尝试"localhost"?

我会上传我的代码,附加图像等。我为我的糟糕代码道歉,但现在我需要解决这个问题

谢谢所有的

public void callingSpreadsheetTest2() throws IOException {
    System.out.println("callingSpreadsheetTest2");
    HttpTransport  transport = new NetHttpTransport();
    JacksonFactory jsonFactory = new JacksonFactory();
    GoogleOAuthParameters oAuthParameters = new GoogleOAuthParameters();
    oAuthParameters.setOAuthConsumerKey(CLIENT_ID);
    oAuthParameters.setOAuthConsumerSecret(CLIENT_SECRET);
    OAuthHmacSha1Signer signer = new OAuthHmacSha1Signer();
    GoogleOAuthHelper oAuthHelper = new GoogleOAuthHelper(signer);
    oAuthParameters.setScope(SCOPES);
    try{
        oAuthHelper.getUnauthorizedRequestToken(oAuthParameters);
    }catch (OAuthException e){
        e.printStackTrace();
    }
    String requestUrl = oAuthHelper.createUserAuthorizationUrl(oAuthParameters);
    System.out.println(requestUrl);
    System.out.println("Please visit the URL above to authorize your OAuth "
     + "request token.  Once that is complete, press any key to "
     + "continue...");
    try{
        System.in.read();
    }catch(IOException e){
        e.printStackTrace();
    }
    oAuthParameters.setOAuthType(OAuthType.TWO_LEGGED_OAUTH);
    String token = null;
    try{
        token = oAuthHelper.getAccessToken(oAuthParameters);
    }catch(OAuthException  e){
        e.printStackTrace(); //---->Attention: HERE I GOT toke=null (oauth_token does not exist.)
    }
    System.out.println("OAuth Access Token: " + token);
    System.out.println();
    URL feedUrl = null;
    try{
        feedUrl = new URL(SPREADSHEET_URL);
    }catch(MalformedURLException e){
        e.printStackTrace();
    }
    SpreadsheetService spreadsheetService = new SpreadsheetService("GAppEngineProj");
    try{
        spreadsheetService.setOAuthCredentials(oAuthParameters, signer);
    }catch(OAuthException e){
        e.printStackTrace();
    }
    try {
        SpreadsheetFeed feed = spreadsheetService.getFeed(feedUrl, SpreadsheetFeed.class);
        List<SpreadsheetEntry> spreadsheets = feed.getEntries();
        if(spreadsheets != null) {            
              for (SpreadsheetEntry spreadsheet : spreadsheets) {
                  System.out.println(spreadsheet.getTitle().getPlainText());
              }
         }
    } catch (ServiceException e) {
        e.printStackTrace();
    }

还有我的类"com.google.gdata.client.spreadsheet。电子表格服务没有setOAuth2Credentials方法。setOAuthCredentials(参数,签名者);]

再次感谢!

您正在使用OAuth1协议的代码,该协议已弃用。你应该使用OAuth2。

一个App Engine应用程序附带了一个非常方便的AppIdentityService(文档),它允许你的应用程序生成OAuth2令牌,就好像应用程序有一个your-project-id@appspot.gserviceaccount.com格式的电子邮件。

你所要做的就是使用Google Drive/Google Spreadsheets UI与这封电子邮件共享你的电子表格。在您的情况下,电子邮件将是fourth-case-XXXX@appspot.gserviceaccount.com

然后在代码中这样做:

AppIdentityService appIdentity = AppIdentityServiceFactory.getAppIdentityService();
String accessToken = appIdentityService.getAccessToken(Collections.singleton(SPREADSHEET_SCOPE)).getAccessToken();
GoogleCredential googleCredential = new GoogleCredential();
googleCredential.setAccessToken(accessToken);
spreadsheetService = new SpreadsheetService("MyApp");
spreadsheetService.setOAuth2Credentials(buildCredential());

然后你准备使用你的spreadsheetService读取数据。

注意:此代码是使用GData库版本1.47.1编写的。

相关内容

最新更新