1

我正在尝试用 Vala 制作一个简单的 Twitter 应用程序。我正在使用 Vala Rest 绑定(librest-dev v0.7)。一切正常,直到我尝试初始化一个OAuthProxyCall,此时我从 vala 编译器得到这个 C 错误:

someone@someone-UBook:~/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 才能正确编译?

4

1 回答 1

0

显然,ProxyCall 没有构造函数。//setup call我将该部分的第一行更改为,ProxyCall call = proxy.new_call();而不是,OAuthProxyCall call = new OAuthProxyCall();这似乎已经摆脱了错误。尽管 Twitter 仍在对我大喊大叫,但它现在可以编译了。

更新:这是正确的代码(带有语法突出显示的版本):

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;
    }
}
于 2012-05-19T13:41:33.193 回答