2012-05-19 154 views
1

我正在嘗試使用Vala製作一個簡單的Twitter應用程序。我正在使用Vala Rest綁定(librest-dev v0.7)。一切正常,直到我嘗試初始化一個OAuthProxyCall,在這一點上,我得到了VALA編譯該C錯誤:Vala Rest OAuthProxy:未定義參考錯誤

[email protected]:~/workspace/vala/twitter$ valac --pkg rest-0.7 TwitterAuthTest.vala -o authtest 
/home/someone/workspace/vala/twitter/TwitterAuthTest.vala.c: In function ‘twitter_auth_test_main’: 
/home/someone/workspace/vala/twitter/TwitterAuthTest.vala.c:206:10: warning: assignment makes pointer from integer without a cast [enabled by default] 
/tmp/ccEzeuFy.o: In function `twitter_auth_test_main': 
TwitterAuthTest.vala.c:(.text+0x4a8): undefined reference to `oauth_proxy_call_new' 
collect2: ld returned 1 exit status 
error: cc exited with status 256 
Compilation failed: 1 error(s), 0 warning(s) 

這裏是我的代碼,只是因爲我可以使它:

using Rest; 

public class TwitterAuthTest { 
    private static const string CONSUMER_KEY = "XXXXXXXXXXXXXXXXXXXXXX"; 
    private static const string CONSUMER_SECRET = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; 
    private static const string URL_FORMAT = "https://api.twitter.com"; 
    private static const string REQUEST_TOKEN_URL = "https://api.twitter.com/oauth/request_token"; 

    private static const string FUNCTION_ACCESS_TOKEN = "oauth/access_token"; 
    private static const string FUNCTION_STATUSES_UPDATE = "statuses/update.xml"; 

    private static const string PARAM_STATUS = "status"; 

    public static int main(string[] args) { 
     // initialize proxy 
     var proxy = new OAuthProxy(CONSUMER_KEY, CONSUMER_SECRET, URL_FORMAT, false); // false = url doesn't require expansion 

     // request token 
     try { 
      proxy.request_token("oauth/request_token", "oob"); 
     } catch (Error e) { 
      stderr.printf("Couldn't get request token: %s\n", e.message); 
     } 

     // prompt user for pin 
     stdout.printf("Go to http://twitter.com/oauth/authorize?oauth_token=%s then enter the PIN\n", proxy.get_token()); 
     string pin = stdin.read_line(); //3001930 

     // access token 
     try { proxy.access_token(FUNCTION_ACCESS_TOKEN, pin); } 
     catch (Error e) { 
      stderr.printf("Couldn't get access token: %s\n", e.message); 
     } 

     // setup call 
     OAuthProxyCall call = new OAuthProxyCall(); 
     call.set_function(FUNCTION_STATUSES_UPDATE); 
     call.add_param(PARAM_STATUS, "Hello from librest!"); 
     try { call.sync(); } catch (Error e) { 
      stderr.printf("Cannot make call: %s\n", e.message); 
     } 

     return 0; 
    } 
} 

我對REST/OAUTH完全不熟悉 - 我如何設置OAuthProxyCall以使其正確編譯?

回答

0

很明顯沒有ProxyCall的構造函數。我將//setup call部分的第一行更改爲ProxyCall call = proxy.new_call();而不是OAuthProxyCall call = new OAuthProxyCall();,似乎已經清除了錯誤。它現在編譯,雖然Twitter仍然對我大喊大叫。

更新:下面是正確的代碼(version with syntax highlighting):

using Rest; 

public class SimpleTweet { 
    private static const string CONSUMER_KEY = "#######################"; // this comes from your app's twitter account page 
    private static const string CONSUMER_SECRET = "########################################"; // this comes from your app's twitter account page 
    private static const string URL_FORMAT = "https://api.twitter.com"; 
    private static const string REQUEST_TOKEN_URL = "https://api.twitter.com/oauth/request_token"; 

    private static const string FUNCTION_ACCESS_TOKEN = "oauth/access_token"; 
    private static const string FUNCTION_STATUSES_UPDATE = "1/statuses/update.xml"; 

    private static const string PARAM_STATUS = "status"; 

    public static int main(string[] args) { 
     // make sure there is a message to tweet 
     if(args[1] == null || args[1] == "") { 
      stdout.printf("No tweet message specified.\n"); 
      return 0; 
     } 

     /** Authenticating with Twitter */ 
     // initialize proxy 
     var proxy = new OAuthProxy(CONSUMER_KEY, CONSUMER_SECRET, URL_FORMAT, false); 

     // request token 
     try { 
      proxy.request_token("oauth/request_token", "oob"); 
     } catch (Error e) { 
      stderr.printf("Couldn't get request token: %s\n", e.message); 
      return 1; 
     } 

     // prompt user for pin 
     stdout.printf("Go to http://twitter.com/oauth/authorize?oauth_token=%s then enter the PIN\n", proxy.get_token()); 
     string pin = stdin.read_line(); 

     // access token 
     try { proxy.access_token(FUNCTION_ACCESS_TOKEN, pin); } 
     catch (Error e) { 
      stderr.printf("Couldn't get access token: %s\n", e.message); 
      return 1; 
     } 

     /** sending the tweet */ 
     // setup call 
     ProxyCall call = proxy.new_call(); 
     call.set_function(FUNCTION_STATUSES_UPDATE); 
     call.set_method("POST"); 
     call.add_param(PARAM_STATUS, args[1]); 
     try { call.sync(); } catch (Error e) { 
      stderr.printf("Cannot make call: %s\n", e.message); 
      return 1; 
     } 

     // print success 
     stdout.printf("Tweet succeeded!\n"); 

     return 0; 
    } 
} 
+2

您可能需要更新您的休息-0.7綁定。 git中的版本(http://git.gnome.org/browse/vala/tree/vapi/rest-0.7.vapi)會阻止你嘗試這個,並導致一個(較不易混淆的)valac錯誤而不是C錯誤。此外,僅供參考,有一個例子可以通過http://git.gnome.org/browse/librest/tree/examples/post-twitter.c在librest上發佈到Twitter,它可以幫助你。它在C中,但你應該能夠知道如何去做。 – nemequ

+0

非常感謝你。你提供的鏈接幫助我解決了我發佈推文的問題。 – weberc2