2012-03-12 68 views
2

注意:您或许可以通过指导我到一个能够解释如何与Web服务进行交互的地方来帮助我。但对于这个特定问题的帮助将不胜感激!OAuth Vimeo with Scribe(Java)

我很难理解OAuth是如何工作的。我正在尝试开发一个桌面应用程序,该应用程序会将一堆视频上传到Vimeo上的帐户。我试着用Scribe APIexamples修改一下。 Scribe不幸有一个Vimeo例子,所以我一直在试图改变Facebook的例子,使它适用于Vimeo。关于这一切如何运作(我已经能够1:找到,2:理解)的信息很少。这是我有尽可能代码和错误:

public class VimeoTest 
{ 
    private static final String NETWORK_NAME = "Vimeo"; 
    private static final Token EMPTY_TOKEN = null; 

    public static void main(String[] args) 
    { 
    // Replace these with your own api key and secret 
    String apiKey = "MYAPIKEY"; 
    String apiSecret = "MYAPISECRET"; 
    OAuthService service = new ServiceBuilder() 
            .provider(VimeoApi.class) 
            .apiKey(apiKey) 
            .apiSecret(apiSecret) 
            .build(); 
    Scanner in = new Scanner(System.in); 

    System.out.println("=== " + NETWORK_NAME + "'s OAuth Workflow ==="); 
    System.out.println(); 
    OAuthRequest orequest = new OAuthRequest(Verb.GET, "http://vimeo.com/api/rest/v2"); 
    orequest.addQuerystringParameter("method", "vimeo.test.null"); 
    Response send = orequest.send(); 
    System.out.println(send.getBody()); 

    // Obtain the Authorization URL 
    System.out.println("Fetching the Authorization URL..."); 
    Token requestToken = service.getRequestToken(); 

    //Breaks on the line above. 
    //But I think it's because the orequest.send() returned a 100 error code 

    String authorizationUrl = service.getAuthorizationUrl(requestToken); 
    System.out.println("Got the Authorization URL!"); 
    System.out.println("Now go and authorize Scribe here:"); 

    //I do NOT want to have to do this. Is there any other way I can have this authorize without going to a web browser to do this? 

    System.out.println(authorizationUrl); 
    System.out.println("And paste the authorization code here"); 
    System.out.print(">>"); 
    Verifier verifier = new Verifier(in.nextLine()); 
    System.out.println(); 

这里的输出和错误:

=== Vimeo's OAuth Workflow === 

1.0 
<?xml version="1.0" encoding="utf-8"?> 
<rsp generated_in="0.0069" stat="fail"> 
    <err code="100" expl="The API key passed was not valid" msg="Invalid API Key" /> 
</rsp> 
Fetching the Authorization URL... 
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/codec/binary/Base64 
    at org.scribe.services.HMACSha1SignatureService.doSign(HMACSha1SignatureService.java:47) 
    at org.scribe.services.HMACSha1SignatureService.getSignature(HMACSha1SignatureService.java:33) 
    at org.scribe.oauth.OAuth10aServiceImpl.getSignature(OAuth10aServiceImpl.java:118) 
    at org.scribe.oauth.OAuth10aServiceImpl.addOAuthParams(OAuth10aServiceImpl.java:63) 
    at org.scribe.oauth.OAuth10aServiceImpl.getRequestToken(OAuth10aServiceImpl.java:43) 
    at autouploadermodel.VimeoTest.main(VimeoTest.java:38) 
Caused by: java.lang.ClassNotFoundException: org.apache.commons.codec.binary.Base64 
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366) 
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423) 
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356) 
    ... 6 more 
Java Result: 1 

不管怎样,我敢打赌,这是很简单,但我只是不明白怎么与Web服务进行交互。谢谢你的帮助!

回答

4

您需要在您的班级路径中包含apache commons codec

+0

好吧,这很奇怪。今天早上我尝试了包括apache commons codec(我实际上觉得愚蠢,因为我不知道该怎么做,但是我发现了一个版本为1.6的jar,并且将它作为一个库包含进来),并且我导入了org.apache.commons .codec.net *;'。然后我跑了它,它把我带到了我需要在Vimeo授权应用程序的地方。我拷贝并粘贴了Vimeo给我的代码,并在这一行上得到了一个空指针:'Token accessToken = service.getAccessToken(EMPTY_TOKEN,verifier);''EMPTY_TOKEN'被声明为'null',所以这是有道理的,但那就是这个例子有......我只是不明白。 – kentcdodds 2012-03-13 13:37:02

+0

哦,另一个奇怪的是,如果我删除了编解码器的导入,它可以正常工作......这就是我说的很奇怪的部分。所以基本上,我的代码看起来和昨天一样,当我得到错误时,但是这次更进一步...我的API密钥在几天前被批准,所以我不能解释为什么这是,但..是的。谢谢您的帮助! – kentcdodds 2012-03-13 13:38:39

+0

只是再次运行它,它仍然说我的API密钥无效,但它获得授权URL。这对我没有任何意义。 – kentcdodds 2012-03-13 14:02:22