gpt4 book ai didi

java - 两条腿的 OAuth 和 Gmail 原子提要

转载 作者:行者123 更新时间:2023-11-30 06:38:01 26 4
gpt4 key购买 nike

我们正试图让 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 方面?

最佳答案

我们认为我们可以使用 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("=====================================================");
}
}

关于java - 两条腿的 OAuth 和 Gmail 原子提要,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2714296/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com