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

标签 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/

但是当我尝试访问该 URL 时

 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 不存在”吗?

最佳答案

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

oauthParameters.setOAuthType(OAuthType.TWO_LEGGED_OAUTH);

这可能会解决您的问题。

关于java - RuntimeException : com. google.gdata.client.authn.oauth.OAuthException:oauth_token 不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4315096/

相关文章:

java - 我的 isEmpty 方法不起作用,我不知道为什么

java - Unresolved 要求 : Require-Bundle: org. eclipse.core.databinding.beans;捆绑版本 ="1.2.200"

java - 避免在JDK升级后实现新的接口(interface)方法

java - 将 TabLayoutPanel 放入 SplitLayoutPanel 时出现空白页面

node.js - 如何配置 OAuth 服务器发送配置文件信息?

java - 具有相同包名称和相同类名称的两个不同的 .jar。如何得到那个?

java - GWT 2.6.0 如何启用 Internet Explorer 6/7 排列

java - 在 java 中使用正则表达式修改 xml

oauth - 无法从 Thinktecture 授权服务器获取 token

c# - C# 中的 OAuth 作为客户端