How to login to twitter via Ajax? - javascript

Sorry if this question has been asked before, I've look for a while and haven't found anything related. I recently started delving into AJAX/Javascript and for fun I was curious if it was possible to log into Twitter via an AJAX call.
So I looked at the source code for the login page, and it looks like on line 198 and 202 (Chrome source code viewer) the username and password keys are set, respectively, as well as the page it's being sent to. So from there I wrote this:
$.post('https://twitter.com/sessions',
{"session[username_or_email]" : "USERNAME",
"session[password]": "PASSWORD"},
function(data) {
$('body').html(data);
});
It returns something along the lines of "Username/Password" are incorrect. Are the fields wrong? Is my approach fundamentally flawed? If so, how could a similar effect be achieved? Thanks!
Edit: Follow up question, if I send an ajax request and that page should create/set a cookie, what happens to that cookie? Does it just evaporate, or is it set for the browser?

Related

Can't get /api/login/ to work - JavaScript / Reddit

i'm running out of attempts... just got this "you are doing that too much. try again in 27 minutes." so I thought I would come here and ask.
This is the request I'm sending
URL:
http://www.reddit.com/api/login/
Headers:
User-Agent: "Reddit test app"
POST DATA:
user=USERNAME&passwd=PASSWORD&api_type=json
Putting the parameters in the URL instead... strangely works! I would like to avoid this though as it may not be safe to pass the password in the URL.
e.g. http://www.reddit.com/api/login/?user=USERNAME&passwd=PASSWORD&api_type=json
Another question... how do I access /api/v1/me/ ?
Tried passing modhash as a GET/POST parameter, and also by passing "uh" as a Header. None of them work.
Any ideas what I'm doing wrong? Thanks! :)
First, you can use https://ssl.reddit.com for login. That makes the concern about passing parameters as part of the URL unnecessary.
Second, /api/v1/me/ can only be used with OAuth2 access that has the 'identity' scope. It cannot be accessed from a logged in session.
As to why your POST request isn't working, you probably have incorrectly encoded the POST data. Try POSTing your data to http://httpbin.org/post and see if you get the expected results back.
i know its an old question, but just want to add an answer just in case some is referred here. I think the problem lies in not using the "content-type" in the header, as a result of which the post data is getting processed correctly. I was making the same mistake and a call ".setRequestHeader("Content-type","application/x-www-form-urlencoded");" fixed it. I think everyone is using frameworks like jQuery that must be doing this internally.

Facebook returns empty data attribute when publishing Open Graph action

I am trying to integrate the new Open Graph objects, actions and aggregations into our site.
I have:
Created an object and an associated action
Added the correct markup to my page
Verified the markup from #2 is valid in the Facebook Debug tool
When trying to submit the action for approval, it tells me I need to publish the action at least once. I tried doing that using the following request as in the documentation:
https://graph.facebook.com/me/<my-namespace>:<my-action>?<my-object>=<url-to-object>&access_token=<my-access-token>
The response didn't give me any errors but responded with an empty data attribute.
{
data: [ ],
paging: {
next: "<paging-url-here>"
}
}
I thought that perhaps the empty data in the response was indirect way to tell me it succeeded but when I look at my profile the action wasn't published, nor will Facebook let me submit the action (they still tell me I need to publish the action at least once).
I've verified the access token is valid, and in fact, I messed with it by removing a few characters and the request fails telling me the access token is invalid.
I'm obviously missing something obvious here, so would love some help!
Facebook has a bug making GET behave differently than POST. If you're experiencing the same problem I am and using a GET, switch to a POST and it should work.
That should work. Please file a bug at developers.facebook.com/bugs for what you're specifically seeing and we'll investigate.

Using POST to send data back to objective c

I am attempting to send data back to my objective c program from javascript.
I found a very helpful question here that got me started, but I quickly ran into a problem: the post seems to be failing.
I also verified syntax at this helpful site.
I know this because I update the contents of a div on my page before and after the post call and I only see the update pre-post. I also have a breakpoint at the beginning of shouldStartLoadWithRequest and it's not firing.
Here's my simple javascript code:
function updateAction(obj) {
document.getElementById("status").innerHTML="sending:"+obj;
$.post("http://actionupdate", {"data":obj});
document.getElementById("status").innerHTML="sent";
}
So.. my question: what am I missing? What's wrong with my post?
From your syntax, I guess you are using jquery? The UIWebViewDelegate's webView:shouldStartLoadWithRequest:navigationType: method does not seem to get called for ajax requests, which is what $.post() does. You have to use a normal post instead.
You may find UIWebViewDelegate not monitoring XMLHttpRequest? helpful.

Updating facebook status with message from a Javascript variable

Is there an easy way to let users update their status on Facebook with a message I have in a JavaScript variable? I know there's a URL you can use to share a link, which looks like:
testlink
That doesn't fill in a status update when it takes them to the share page. Is there a way I can have that pre-filled, or have their status automatically updated?
Thanks!
Edit: Not quite what I'm going for. I can use PHP if necessary (although I have very little experience with it), but I don't want to have to bug users with allowing an application, or have to deal with actually writing a facebook app. I just want to give users a premade status update, so all they have to do is click the 'share' button. Thanks again!
Edit 2: So the api looks promising, but a little complicated for me. It looks like it would involve getting permission from the user to set their status, and possibly something about setting up a cross-communications channel to let my site communicate with facebooks. Assuming I can do that, how do I go about using the Facebook Javascript api? I get the code (user_setStatus looks easy enough), but how do I tell my script that I'm using Facebook's api? Thanks again...again!
Try this:
http://www.facebook.com/connect/prompt_feed.php?&message=my%20awesome%20status
[EDIT]
Link is not working please update it.
Is the facebook javascript API not what you're looking for? It seems to expose the entire REST-ful API as a javascript library.
users_setStatus(...) looks promising.
Not sure if you're using PHP but here's a link that shows you how to use the Facebook API to update the status. You could call a page with Ajax from Javascript, passing in the variable and have that posted to the Facebook status message.
http://fbcookbook.ofhas.in/2009/02/07/facebook-reveals-status-api-how-to-use-it/
Using just mcqwerty solution you'll get an annoying resize screen effect.
But, you can add a parameter in order to avoid this flickering. Display touch will avoid this.
https://www.facebook.com/connect/prompt_feed.php?display=touch&message=Hiall
Here is working example which requires application ID, but fully working and it is easy to implement:
<a href="http://www.facebook.com/dialog/feed?app_id=123050457758183&link=http://www.build.server.com&picture=http://fbrell.com/f8.jpg&
name=Some%20Encoded&Message=Reference%20Documentation&
description=Using%20Dialogs%20to%20interact%20with%20users.&
redirect_uri=https://arapskirjecnik.web44.net/"> Share with FB</a>
see more here
here is the way which works and it doesn't requires facebook application id:
<a onClick="window.open('http://www.facebook.com/sharer.php?s=100&p[title]=Title&p[summary]=summary&p[url]=www.build.server.com&p[images][0]=http://fbrell.com/f8.jpg','sharer','toolbar=0,status=0,width=548,height=325');" href="javascript: void(0)">Insert text or an image here.</a>
As you can see from above code you can enter custom title, summary, link and images.
and if you want to automate, to share current link, youst replace this part of the code in upper example:
p[url]='+ document.URL +'&p[images]

header(Location:) via AJAX not redirecting

First off, let me say that I know this does not seem like an uncommon issue. I do not intend to clutter these forums with questions that have already been answered, but, I have worked through probably about 3 dozen threads with similar issues and have been unable to reach a solution.
The short description of my issue is this: I am trying to redirect after submitting a form using php's header(location:) function, and it's not redirecting. It is a little bit complicated because the form is submitted via AJAX. The steps my code goes through are as follows:
User submits form > form data sent via AJAX > form processing page validates data > if valid, form processing page inserts data into database > if submission is successful, form processing page adds redirect URL to $_SESSION > form processing page returns a 'redirect' variable > javascript/AJAX checks for redirect variable, and refreshes page if found > page header checks $_SESSION for redirect URL, and if found, sets appropriate headers and exits.
I guess the first thing I want to ask is, is this the right way of going about this? It seems to me that there should be a simpler way of doing this. It's obviously much simpler to pass the redirect URL to the javascript and do a window.location redirect, but I read that it's much better/more secure to handle that on the server side, so I'm trying to do that.
Assuming I'm going about this in the right direction, what could be going wrong here? I've checked for leading and trailing whitespace, BOM characters, I've even tried output buffering and I still have the same issue.
What happens on form submission is, the page refreshes, but it returns to the original form page rather than the redirect url. I have turned on the most detailed error reporting and get no errors at all.
One thing that may or may not be of interest, I have an error_log function set up to log all headers to a file right after I set the Location: header. When I redirect outside of AJAX (which works), the accept: header is set to html, but when I try to do it via AJAX, the accept header is set to JSON. Could that cause a problem?
Thank you so much for taking the time, and again, apologies if this is a dumb question. I have used this forum for years and have never once had to post a question on it because it has always solved my problems until now. Thanks in advance!
PHP is too generous to include in your code not only HTML code, but also JavaScript code. I'll explain one thing. When you send data by ajax, it is often difficult return Boolean data (whether true or false, depending on the conditions we work side server with php in some file established in our direction ajax) to give a final answer.
On the other hand, returning to the generosity of PHP, always when we try to validate data server-side and through ajax, we can manipulate the section where work freely to show some response message, a redirect to somewhere on your page or create a session. Anyway, whatever.
What I mean is that in your controller section where you will work with AJAX, you can set different conditions. That is, if the user data are correct, then you could send to call a script from Javascript to redirect him with a location.reload (); and also assign a session automatically. If the user does not have the correct data, then what we should do is return a warning message or alert to the exit AJAX, usually it goes after a:
function (response) {
$ ('.answer').html(response);
}
Note that it is not necessary require, for example, a $ ('.answer').html(response); to return an answer, because ajax only sends data to the server and if there is a reply message well, and if not well. Many times what I do, is to send messages via console, although it is often convenient to do so, because as often work with several conditions and that depends on the result of our request, we will be forced to show some response in some element within our code.
What I advise you is that you work javascript in PHP, and hence redirect to other pages, assign sessions or simply return a message. Remember that an ajax request is not only asynchronous, but repetitive and can send it to call as often as you need. The fact that you sent your ajax call php file and you have returned an answer, does not mean you can go back to work with it once you finish your ajax request.
It is the right way to do what you want, it is often easier to manipulate our server-side code that client side. Greetings.

Categories

Resources