1

I'm trying to make a simple Twitter App with Vala. I'm using the Vala Rest bindings (librest-dev v0.7). Everything works until I try to initialize an OAuthProxyCall, at which point I get this C error from the vala compiler:

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)

Here is my code as simply as I could make it:

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;
    }
}

I'm completely unfamiliar with REST/OAUTH--how do I need to setup my OAuthProxyCall in order to make this compile correctly?

weberc2
  • 7,423
  • 4
  • 41
  • 57

1 Answers1

0

Evidently there is no constructor for a ProxyCall. I changed the first line of the //setup call section to ProxyCall call = proxy.new_call(); instead of OAuthProxyCall call = new OAuthProxyCall(); and that seems to have gotten rid of the error. It now compiles although Twitter is still yelling at me.

Update: Here is the correct code (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;
    }
}
weberc2
  • 7,423
  • 4
  • 41
  • 57
  • 2
    You might want to update your rest-0.7 bindings. The version in git (http://git.gnome.org/browse/vala/tree/vapi/rest-0.7.vapi) would have prevented you from trying this and resulted in a (less confusing) valac error instead of a C error. Also, just FYI, there is an example of posting to Twitter in librest at http://git.gnome.org/browse/librest/tree/examples/post-twitter.c which may help you. It's in C, but you should be able to get an idea of how to do it. – nemequ May 19 '12 at 17:38
  • Thank you SO much. The link you provided helped me sort out my issues with tweet posting. – weberc2 May 19 '12 at 21:47