RuntimeException:com.google.gdata.client.authn.oauth.OAuthException:oauth_token不存在

时间:2010-11-30 15:00:06

标签: java gwt oauth google-apps google-apps-marketplace

我正在尝试在Google Apps域上获取用户的管理状态。

因此,我将从Google Marketplace的应用程序列表中获取的CONSUMER_KEY和CONSUMER_SECRET放入我的代码中。

String CONSUMER_KEY = "748096634522.apps.googleusercontent.com";
String CONSUMER_SECRET = "PK/Wx/9sbLkz5hhj6rq6LKCZ";

我已授权该应用程序访问范围

https://apps-apis.google.com/a/feeds/user/

但是当我尝试访问网址时

 https://apps-apis.google.com/a/feeds/user/2.0/domain.com/username%40domain.com

我收到以下错误:

javax.servlet.ServletContext log: Exception while dispatching incoming RPC call
com.google.gwt.user.server.rpc.UnexpectedException: Service method 'public abstract com.icada.idea.client.LoginInfo com.icada.idea.client.LoginService.login(java.lang.String,java.lang.String)' threw an unexpected exception: java.lang.RuntimeException: com.google.gdata.client.authn.oauth.OAuthException: oauth_token does not exist.

然而,尝试Googles OAuth playground可以很好地完成上述数据。

这是我的代码:

private boolean isAdmin (String userEmail, String domain) {
  boolean ret= false;

  String CONSUMER_KEY = "748096634522.apps.googleusercontent.com";
  String CONSUMER_SECRET = "PK/Wx/9sbLkz5hhj6rq6LKCZ";

  GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters();
  oauthParameters.setOAuthConsumerKey(CONSUMER_KEY);
  oauthParameters.setOAuthConsumerSecret(CONSUMER_SECRET);
  oauthParameters.setScope ("https://apps-apis.google.com/a/feeds/user/");

  com.google.gdata.client.appsforyourdomain.UserService client
    = new com.google.gdata.client.appsforyourdomain.UserService ("Idea");
  try {
    client.setOAuthCredentials(oauthParameters, new OAuthHmacSha1Signer());
    URL feedUrl = new URL("https://apps-apis.google.com/a/feeds/user/2.0/"
      + domain +"/"+ java.net.URLEncoder.encode (userEmail));

    UserFeed resultFeed = client.getFeed(feedUrl, UserFeed.class);
    for (UserEntry entry : resultFeed.getEntries()) {
      if (entry.getLogin ().getAdmin ().equals (Boolean.TRUE)) ret= true;
      else ret= false;
    }
  }
  catch (OAuthException ex) {
    log.severe (ex.getMessage () + ex.toString () + ex.getCause ()
      + ex.getStackTrace ().toString ());
    ex.printStackTrace();
  }
  [more catch-blocks...]

  return ret;
}

任何想法,为什么它说“oauth_token不存在”?

1 个答案:

答案 0 :(得分:2)

将此行添加到您的代码中。

oauthParameters.setOAuthType(OAuthType.TWO_LEGGED_OAUTH);

这可以解决您的问题。