oauth-io / oauth-phonegap

OAuth.io plugin for Apache Cordova/PhoneGap
195 stars 69 forks source link

How to get the user_id for twitter sharing? #61

Closed elius1234 closed 8 years ago

elius1234 commented 8 years ago

Twitter sharing not working can anyone help? I am using the below code while executing the fail function is invoked and i am a html page in the response. OAuth.initialize("pv-0pAxUwYECGUf4vhkjhv8");
OAuth.popup('twitter', {cache: true}).done(function (result) {

       result.post('/message', {
            data: {
                user_id: 93,
                content: 'Hello Mr. 93 !'
            }
        })
        .done(function (response) {
            //this will display the id of the message in the console
            alert(response.id);
            console.log(response.id);
        })
        .fail(function (err) {
            //handle error with err
       alert("error"+ JSON.stringify(err));
        });

   })

.fail(function (e) { console.log('error: ' + e.message);

});

thyb commented 8 years ago

/message is not a valid endpoint in the twitter API. I'm not sure where you found the documentation for '/message' but it seems this endpoint does not exists. If I'm right, it's for this very reason you're receiving HTML (which is their 404 page).

All availble endpoints for the Twitter API are described here: https://dev.twitter.com/rest/public

so basically, to post a tweet in the Twitter Timeline (https://dev.twitter.com/rest/reference/post/statuses/update), it would be:

result.post({
    url: '/1.1/statuses/update.json',
    data: {
        status: 'Hello World'
    }
}).done(function(data) {
    //todo when the tweet is posted
})

To post a direct message (https://dev.twitter.com/rest/reference/post/direct_messages/new), it would be:

result.post({
    url: '/1.1/direct_messages/new.json',
    data: {
        user_id: 93,
        text: 'Hello Mr. 93!'
    }
}).done(function(data) {
    //todo when direct message is posted
})

To get the user_id of the authorized user (as the title mention), you can use me() to get unified user informations (http://docs.oauth.io/?Javascript#user-data-api):

result.me().done(function(me) {
    //todo with me.id
})
elius1234 commented 8 years ago

Thanks for the help thyb, it's working now.