views:

334

answers:

3

I am trying to create Google Chrome extension that will send interesting links to twitter. But, I am unable to connect to the Twitter. The server gets my request, but the response is always the same: "You do not have permission to access /1/statuses/update.json on this server (403 Forbidden)." I am using Wireshark to inspect Http responses. Here is the code of the function i am using:

function setStatus(msg) {
var status = "status="+msg;
var client = new XMLHttpRequest();
client.open("POST","http://api.twitter.com/1/statuses/update.json");
client.setRequestHeader("Authorization", "Basic <user credentials>");
client.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
client.onreadystatechange = function(){
        if(client.readyState ==4){
            //whatever
        }
    }
client.send(status);

Am I doing something wrong? I am using Basic Auth. I use this tool to do the encoding, and just put the result instead of "user credentials" part. Also, can someone give me an example of OAuth?

A: 

Use /account/verify_credentials to check your authentification first, you might did some mistakes with the encoding, or the password is simply wrong.

poke
I tried your solution - I am still getting 403 response code. Either I am constantly making some mistake with authentication header, or something else is wrong :(
PainBringer
A: 

You should also be able to do:

client.open("POST","https://screen_name:[email protected]/1/statuses/update.json");

and not worry about changing the request headers.

abraham
+1  A: 

I have already posted what was the problem, but for some reason, that post is missing. Nevermind, here is what happened: The code sample I gave in the first post was correct, but I made a mistake in the manifest file. The manifest file contains the "permissions" section - I needed to list all of the links that extension was going to use. I forgot to list "http://api.twitter.com" and that was the problem. When I added that link, everything started working. Thanks for suggestions and responses.

PainBringer