【问题标题】:Get access google spreadsheet (saved into google drive account) from a java google app engine (servlet)从 java google 应用程序引擎(servlet)获取访问 google 电子表格(保存到 google drive 帐户)
【发布时间】:2015-06-04 16:45:11
【问题描述】:

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

当然,我已经在“谷歌云开发者控制台”中创建了我的项目,我得到了“项目 ID”fourth-case-XXXX 等。直到这一步,我都明白了一切并且进展顺利。

我一直在这里,在谷歌等上查看数千个示例。对于我阅读的所有内容,我都明白:我必须创建一个“OAuth2 凭据”(我在“Api 和身份验证”中创建 OAuth2 凭据的原因)当然是我的开发者控制台(我有一个带有 auth_uri、client_secret、client_id 等的 json)。

根据我的阅读,只需要 client_secret 和 client_id。但是按照本教程(link)我得到了这个错误“oauth_token 不存在。”。在其他教程中,我读到没有必要在应用引擎中使用“OAuth2”。我很头晕。

我唯一想要的是一个简单的 java servlet(没什么复杂的,遵循良好的做法,我不在乎)它从谷歌电子表格(保存到我的谷歌驱动器帐户中)读取数据并通过“jsp”显示,至少我很乐意在我的 servlet 上接收电子表格数据,稍后我会想象如何显示

我正在使用 eclipse luna,我安装了“gdata java api”、“google app engine as localhost”等(全部正确安装,运行没有错误)。创建我遵循this教程的环境

一些问题: 1-电子表格需要发布吗? (菜单:文件-> 发布到网络)。从应用引擎获取访问权限? 2-在强制上传到 Google App Engine (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.SpreadsheetService”也没有方法 setOAuth2Credentials only [service.setOAuthCredentials(parameters, signer);]

再次感谢!!

【问题讨论】:

    标签: java google-app-engine servlets


    【解决方案1】:

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

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

    您所要做的就是使用 Google 云端硬盘/Google 电子表格 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 编写的。

    【讨论】:

    • 感谢您的帮助,对不起我的错误我是初学者。什么样的对象是“appIdentityService”??? ---> AccessTokenRequest atr = GTAuthorizationCode.createAccessTokenRequest (OAuth2, authorizationCode, redirect_uri); ???谢谢
    • 对不起,我忘了添加appIdentityService 实例化代码。我已经用这个信息编辑了代码。 AppIdentityService是 App Engine 提供的一个类。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多