使用oauth获取电子邮件时出错

时间:2012-03-13 12:48:24

标签: oauth-2.0

我正在努力让双腿OAuth与Gmail原子Feed一起使用。我正在使用正确的密钥和密码,并且已经使用任务和日历进行了测试,但同样不适用于电子邮件 代码片段是:

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 = "key";  
public static String CONSUMER_SECRET = "secret";   
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 = "name";                                
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("message==============="+message);
System.out.println("=====================================================");     
System.out.println(" message: " + message.readBodyAsString());       
System.out.println("=====================================================");   
} 

} 

调用invoke函数时出现异常

request: OAuthMessage(GET, https://mail.google.com/mail/feed/atom?xoauth_requestor_id=myid@mydomain.com, [oauth_consumer_key=mykey, oauth_signature_method=HMAC-SHA1, oauth_timestamp=1331642083, oauth_nonce=118947882606867, oauth_version=1.0, oauth_signature=xbqiCZWqFNjX3mk1dbN8zuT%2BTMs%3D])
Exception in thread "main" java.lang.IllegalAccessError: tried to access class org.apache.http.client.methods.HttpRequestBase from class net.oauth.client.httpclient4.HttpClient4
at net.oauth.client.httpclient4.HttpClient4.execute(HttpClient4.java:86)
at net.oauth.client.OAuthClient.access(OAuthClient.java:317)
at net.oauth.client.OAuthClient.invoke(OAuthClient.java:304)
at com.a2o.service.collaboration.common.GmailAtomFeed2LeggedOauth.main(GmailAtomFeed2LeggedOauth.java:62)

知道我为什么会遇到这个例外。

0 个答案:

没有答案