两条腿的 OAuth 和 Gmail Atom feed

发布于 2024-08-30 11:02:18 字数 546 浏览 4 评论 0原文

我们正在尝试让 2-legged OAuth 与 Gmail Atom feed 一起使用。我们使用 John Kristian、Praveen Alavilli 和 Dirk Ba​​lfanz 贡献的 Java 库。 [http://oauth.net/code/] 而不是 GData 库。

我们知道我们有正确的 CONSUMER_KEY 和 CONSUMER_SECRET 等,因为它适用于联系人供稿 (http://www.google.com/m8/feeds/contacts/default/full)并且没有任何问题。然而,对于 Gmail Atom feed,它总是返回: HTTP/1.1 401 Unauthorized

有什么想法吗?我们应该尝试不同的 OAuth 框架还是问题出在 Google 方面?

We're trying to get 2-legged OAuth to work with the Gmail atom feed. We're using the Java library contributed by John Kristian, Praveen Alavilli and Dirk Balfanz. [http://oauth.net/code/] instead of the GData library.

We know we have the correct CONSUMER_KEY and CONSUMER_SECRET, etc. becuase it works with the Contacts feed (http://www.google.com/m8/feeds/contacts/default/full) and have no problems. However with Gmail atom feed it always returns: HTTP/1.1 401 Unauthorized

Any ideas? Should we try a different OAuth framework or does the problem lie on the Google side?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嘦怹 2024-09-06 11:02:19

我们认为它可以与 OAuth 库一起使用,但不能与 GData 库一起使用。

代码片段是:

import static net.oauth.OAuth.HMAC_SHA1;                                                       
import static net.oauth.OAuth.OAUTH_SIGNATURE_METHOD;                                          

import java.net.URL;
import java.util.List;
import java.util.Map; 

import net.oauth.OAuthAccessor;
import net.oauth.OAuthConsumer;
import net.oauth.OAuthMessage; 
import net.oauth.ParameterStyle;
import net.oauth.SimpleOAuthValidator;
import net.oauth.client.OAuthClient;  
import net.oauth.client.httpclient4.HttpClient4;


/**
 * Sample application demonstrating how to do 2-Legged OAuth in the Google Data
 * Java Client.  See the comments below to learn about the details.            
 *                                                                             
 */                                                                            
public class GmailAtomFeed2LeggedOauth {                                       

    public static String CONSUMER_KEY = "test-1001.com";
    public static String CONSUMER_SECRET = "zN0ttehR3@lSecr3+";
    public static String SCOPE = "https://mail.google.com/mail/feed/atom";
    public static String RESOURCE_URL = "https://mail.google.com/mail/feed/atom";
    public static String SERVICE_NAME = "mail";                                  
    public static String username = "username";                                  
    public static boolean debug = true;                                          

    public static void main(String[] args) throws Exception {

        // This should be passed in as a parameter
        String user = username + "@" + CONSUMER_KEY;

        OAuthConsumer consumer = new OAuthConsumer(null, CONSUMER_KEY, CONSUMER_SECRET, null);
        OAuthAccessor accessor = new OAuthAccessor(consumer);                                 

        // HMAC uses the access token secret as a factor,
        // and it's a little less compute-intensive than RSA.
        accessor.consumer.setProperty(OAUTH_SIGNATURE_METHOD, HMAC_SHA1);

        // Gmail only supports an atom feed
        URL atomFeedUrl = new URL(SCOPE +"?xoauth_requestor_id=" + user);

        System.out.println("=====================================================");
        System.out.println("Building new request message...");

        OAuthMessage request = accessor.newRequestMessage(OAuthMessage.GET, atomFeedUrl.toString(),null);

        if (debug) {
            List<Map.Entry<String, String>> params = request.getParameters();
            for (Map.Entry<String, String> p : params) {
                System.out.println("'" + p.getKey() + "' =  <" + p.getValue() + ">");
            }
            System.out.println("Validating message...");
            SimpleOAuthValidator validator=new SimpleOAuthValidator();
            validator.validateMessage(request,accessor);
        }

        OAuthClient client = new OAuthClient(new HttpClient4());

        System.out.println("Client invoking request message...");
        System.out.println(" request: " + request);
        OAuthMessage message = client.invoke(request, ParameterStyle.AUTHORIZATION_HEADER);

        System.out.println("=====================================================");
        System.out.println(" message: " + message.readBodyAsString());
        System.out.println("=====================================================");
    }
}

We think we got it working with the OAuth libraries but not with the GData library.

Snippet of code is:

import static net.oauth.OAuth.HMAC_SHA1;                                                       
import static net.oauth.OAuth.OAUTH_SIGNATURE_METHOD;                                          

import java.net.URL;
import java.util.List;
import java.util.Map; 

import net.oauth.OAuthAccessor;
import net.oauth.OAuthConsumer;
import net.oauth.OAuthMessage; 
import net.oauth.ParameterStyle;
import net.oauth.SimpleOAuthValidator;
import net.oauth.client.OAuthClient;  
import net.oauth.client.httpclient4.HttpClient4;


/**
 * Sample application demonstrating how to do 2-Legged OAuth in the Google Data
 * Java Client.  See the comments below to learn about the details.            
 *                                                                             
 */                                                                            
public class GmailAtomFeed2LeggedOauth {                                       

    public static String CONSUMER_KEY = "test-1001.com";
    public static String CONSUMER_SECRET = "zN0ttehR3@lSecr3+";
    public static String SCOPE = "https://mail.google.com/mail/feed/atom";
    public static String RESOURCE_URL = "https://mail.google.com/mail/feed/atom";
    public static String SERVICE_NAME = "mail";                                  
    public static String username = "username";                                  
    public static boolean debug = true;                                          

    public static void main(String[] args) throws Exception {

        // This should be passed in as a parameter
        String user = username + "@" + CONSUMER_KEY;

        OAuthConsumer consumer = new OAuthConsumer(null, CONSUMER_KEY, CONSUMER_SECRET, null);
        OAuthAccessor accessor = new OAuthAccessor(consumer);                                 

        // HMAC uses the access token secret as a factor,
        // and it's a little less compute-intensive than RSA.
        accessor.consumer.setProperty(OAUTH_SIGNATURE_METHOD, HMAC_SHA1);

        // Gmail only supports an atom feed
        URL atomFeedUrl = new URL(SCOPE +"?xoauth_requestor_id=" + user);

        System.out.println("=====================================================");
        System.out.println("Building new request message...");

        OAuthMessage request = accessor.newRequestMessage(OAuthMessage.GET, atomFeedUrl.toString(),null);

        if (debug) {
            List<Map.Entry<String, String>> params = request.getParameters();
            for (Map.Entry<String, String> p : params) {
                System.out.println("'" + p.getKey() + "' =  <" + p.getValue() + ">");
            }
            System.out.println("Validating message...");
            SimpleOAuthValidator validator=new SimpleOAuthValidator();
            validator.validateMessage(request,accessor);
        }

        OAuthClient client = new OAuthClient(new HttpClient4());

        System.out.println("Client invoking request message...");
        System.out.println(" request: " + request);
        OAuthMessage message = client.invoke(request, ParameterStyle.AUTHORIZATION_HEADER);

        System.out.println("=====================================================");
        System.out.println(" message: " + message.readBodyAsString());
        System.out.println("=====================================================");
    }
}
×纯※雪 2024-09-06 11:02:19

将 OAuth 数据放在授权标头中,而不是放在 URI 中。

Put the OAuth data in the Authorization header, not on the URI.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文