令牌无效-无效令牌:两条腿的OAuth的无效用户



我正在尝试使用OAuth2.0访问google文档。我已经从Google API控制台获得了客户端ID和密钥。但是当我运行这个代码时,我得到了异常。如果我遗漏了什么,你能给我建议吗。。

String CONSUMER_KEY = ".....apps.googleusercontent.com";
        String CONSUMER_SECRET = "sM52Mts_d7snVIomnJaQkjkA";
        GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters();
        oauthParameters.setOAuthConsumerKey(CONSUMER_KEY);
        oauthParameters.setOAuthConsumerSecret(CONSUMER_SECRET);
        DocsService client = new DocsService("testing");
        client.setOAuthCredentials(oauthParameters, new OAuthHmacSha1Signer());
        // Retrieve user's list of Google Docs
        String user = "xesunny@gmail.com";
        URL feedUrl = new URL("https://docs.google.com/feeds/default/private/full" +
                              "?xoauth_requestor_id=" + user);
        DocumentListFeed resultFeed = client.getFeed(feedUrl, DocumentListFeed.class);
        for (DocumentListEntry entry : resultFeed.getEntries()) {
          System.out.println(entry.getTitle().getPlainText());
        }

例外:

com.google.gdata.util.AuthenticationException: Token invalid - Invalid token: Invalid user for the two legged OAuth

令牌无效-无效令牌:两条腿的OAuth的无效用户

令牌无效-无效令牌:两条腿的OAuth的无效用户

错误401

xoauth_requestor_id参数仅适用于用于对Google Apps用户进行身份验证的2位Oauth(2LO)。我认为使用2LO对普通gmail用户进行身份验证是不可能的。

你看到这里的3格Oauth的例子了吗http://code.google.com/apis/gdata/docs/auth/oauth.html#Examples

看起来您使用的客户端ID不是服务帐户。仅在服务帐户上支持两条腿的身份验证。如果您创建了一个新的服务帐户并使用其客户端ID和密钥(RSA私钥),那么您的代码应该可以工作。

最新更新