Oauth return to same page after success/failure, How? - grails

I have this in my config
callback = "http://xxx/oauth/facebook/callback"
successUri = "http://xxx/oauthCallBack/facebookSuccess"
failureUri = "http://xxx/oauthCallBack/facebookFailure"
and its working fine.
now the problem is, I have a sing in link in header which is shared amongst all pages, and when user click on it and authenticates using oauth, I want user to come back on same page from where he/she went for authentication.
How can this be done?
Any help much appreciated.
Thanks.

solved it myself.
Tried using referrer, but had some issues with it.
So
added hidden field with id=currentURL to form, and onLoad of body, populated this hidden field using javascript, eg:
function initParams() {
document.getElementById('currentURL').value = location.protocol + '//' + location.host + location.pathname;
}
and now I have currentURL in params in controller.

Related

Mautic: How to tag users via a landing page?

I have created a campaign form in Mautic and used the manual copy feature to integrate it into my HTML landing page (this is a landing page I have made in HTML. It is not a Mautic landing page).
In Mautic, users are saved as a "contact". The contact has a field called "tags".
I would like to make use of the Mautic Tracking Javascript so I can send a value to this tag field from my HTML landing page.
Here is my code:
< script >
(function(w, d, t, u, n, a, m) {
w['MauticTrackingObject'] = n;
w[n] = w[n] || function() {
(w[n].q = w[n].q || []).push(arguments)
}, a = d.createElement(t),
m = d.getElementsByTagName(t)[0];
a.async = 1;
a.src = u;
m.parentNode.insertBefore(a, m)
})(window, document, 'script', 'http://newsletter.smile.ws/mtc.js', 'mt');
mt('send', 'pageview', {
'tags': 'sunshine-book'
});
< /script>
To test it out, I submitted a test contact on my HTML landing page.
When I log-into Mautic and go to the contact page for the contact, the tag field is blank:
However, the tag does appear as an option in the tag menu:
How can I make it so the tag field is automatically filled with the tag?
The user renzof of the Mautic forum tested my code and it worked for him, but it didn’t work for me. I think I'm doing something wrong, but I can't figure it. Any ideas? Thanks!
The issue is the use of Http. The landing page uses https, but the code was using http (the s is missing), which is why it doesn't work. I updated all my code to use https and it worked.
there is actually nothing wrong with your code, I tested it and the only problem was browser blocking the request due to cors origin, other than that it worked fine.
But I have a hunch in case it doesn't work, i noticed the tags is actually plural and in mautic contact form it taken as array, so if cross domain issue isn't there at your end, can you try tags[] instead tags. However i think normal way should have worked.

Changing CurrentAccessToken in Facebook Unity SDK

I'm trying to post to a Facebook page AS the page using the Unity Facebook SDK running on iOS. As I understand, to do that, I need the pages access token with manage_pages and publish_pages. I know that I can get it from /me/accounts?fields=access_token, but how do I tell AccessToken.CurrentAccessToken to use my pages access token instead?
Right now i'm using the following:
var wwwForm = new WWWForm();
//wwwForm.AddField ("access_token", "A-T I NEED");
wwwForm.AddBinaryData("image", screenshot, "InteractiveConsole.png");
wwwForm.AddField("message", "herp derp. I did a thing! Did I do this right?");
FB.API("/PAGE-ID/photos", HttpMethod.POST, HandleResult, wwwForm);
I tried putting the access token manually, but that didn't work (so I commented it out).
With this as it is I'm getting an error, telling me that I need publish_actions, wich is not correct since I'm not trying to post as the user. If I also get publish_actions the Post goes online, but is posted to the page as the user speaking. (User is also Admin)
Any Ideas ? Thanks!
So, I filed a bug report to facebook and as it turns out: "… at this time this functionality is not supported." Wich simply means there is now way to use the Page Access Token you acquired via the FB.API within the FB.API. And they are not going to tell you abot it in the documentation.
As a workaround I simply use a UnityWebRequest like this:
IEnumerator UploadToPage(byte[] screenshot) {
var wwwForm = new WWWForm();
wwwForm.AddField("message", "herp derp. I did a thing! Did I do this right?");
wwwForm.AddBinaryData("image", screenshot, "Test.png");
string url = "https" + "://graph.facebook.com/"+ PageID + "/photos";
url += "?access_token=" + PageAccessToken;
using (UnityWebRequest www = UnityWebRequest.Post(url, wwwForm))
{
yield return www.Send();
if (www.isError)
{
Debug.Log(www.error);
}
else
{
Debug.Log("Form upload complete!");
}
}
Debug.Log(url);
}

NHtmlUnit cannot submit button

I'm discovering about NHtmlUnit to build an application web. But when I try it with auto login to yahoo mail. But after I run code. I refresh the login page of Yahoo so nothing changed. Not logged.
Code:
NHtmlUnit.WebClient driver = new NHtmlUnit.WebClient();
driver.Options.JavaScriptEnabled = true;
driver.Options.ThrowExceptionOnScriptError = false;
driver.Options.ActiveXNative = true;
driver.Options.CssEnabled = true;
HtmlPage page = driver.GetHtmlPage("https://login.yahoo.com/config/login?");
HtmlForm form = page.GetFormByName("login_form");
HtmlTextInput user = (HtmlTextInput)form.GetInputByName("login");
HtmlPasswordInput pass = (HtmlPasswordInput)form.GetInputByName("passwd");
user.SetValueAttribute("my account");
pass.SetValueAttribute("my pass");
HtmlSubmitInput submitButton = (HtmlSubmitInput)page.GetElementByName(".save");
HtmlPage nextpage = (HtmlPage)submitButton.Click();
Please help me why. I write it on .NET MVC 4 C#. Thank you very much.
Only update below single line,due to which u may face class cast exception
HtmlButton submitButton = (HtmlButton)page.GetElementByName(".save");
The code is completely fine, and should works.
Probably the reason you think it is not, is that you check it by a wrong method!
One essential elements which makes a site (i.e. login.yahoo.com) see you as logged in user, is the cookies it provide to your browser/webdriver when you use login forms, so if you use the login procedure by an instance of NHtmlUnit.Webclient (i.e. driver) then you are only logged in inside the driver web client, not inside any other browser or client, even on the same machine.
I tried to add this as a comment, but I don't have enough reputations.

Rails 3: How to tell if a user has visited a page before?

I have a Rails app (using Authlogic for authentication) with a simple jquery animation that I only want to run once upon first page load. I know that the key is to check cookies or something to see if the user has visited the page before. Please forgive my n00bness, I know little to nothing about HTTP cookies or sessions.
So, what's the best way to see if a visiting user (even if they haven't logged in) is viewing a page for the first time?
EDIT: Ok, so I realize I wasn't being entirely clear.
I've spent hours looking at similar questions and reading the Rails API Docs for cookies and sessions and I still can't visualize how to implement a visited? function for each page in my site that will only be set to "true" after the user has visited the page the first time. I looked at the supposed "duplicate" question Rails Detect If User's Very First Visit and the respective answers and still can't figure it out.
Here's my "Pages" controller:
def home
#title = "Home"
end
def contact
#title = "Contact Us"
end
dd
And my jquery javascript that does a simple animation:
$(document).ready(function()
{
if (!$.cookie('visited')) {
$('.title .flying-text').css({opacity:0});
$('.title .active-text').animate({
opacity:1,
marginTop: "-150px",
}, 5000);
}
});
I only want it to show if the user HAS NOT visited the page before. I have no idea how to properly set a cookie in Rails, nor where to put it, nor how to make sure that the animation script can access that exact same cookie value. Can someone give me a hand?
~Dan
You can set a cookie like this:
cookies[:welcomed] = {:value => true, :expires => Time.now + 6.months}
and from jquery make a wee function
function readCookie(name) {
var nameEQ = name + "=", ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i += 1) {
var c = ca[i];
while (c.charAt(0) === ' ') {
c = c.substring(1, c.length);
}
if (c.indexOf(nameEQ) === 0) {
return c.substring(nameEQ.length, c.length);
}
}
return null;
}
and call it #in js
if (readCookie('welcomed') == null){}
Ignore the cookie from Javascript.
Using the question referenced as a duplicate, use a permanent cooke and on the first run, set a class on your body from Rails (let's say first-run). Then, have your javascript check and see if body has class first-run. At that point, you can have javascript execute it's first run code.

Twitter O-Auth Callback url

I am having a problem with Twitter's oauth authentication and using a callback url.
I am coding in php and using the sample code referenced by the twitter wiki, http://github.com/abraham/twitteroauth
I got that code, and tried a simple test and it worked nicely. However I want to programatically specify the callback url, and the example did not support that.
So I quickly modified the getRequestToken() method to take in a parameter and now it looks like this:
function getRequestToken($params = array()) {
$r = $this->oAuthRequest($this->requestTokenURL(), $params);
$token = $this->oAuthParseResponse($r);
$this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
return $token;
}
and my call looks like this
$tok = $to->getRequestToken(array('oauth_callback' => 'http://127.0.0.1/twitter_prompt/index.php'));
This is the only change I made, and the redirect works like a charm, however I am getting an error when I then try and use my newly granted access to try and make a call. I get a "Could not authenticate you" error. Also the application never actually gets added to the users authorized connections.
Now I read the specs and I thought all I had to do was specify the parameter when getting the request token. Could someone a little more seasoned in oauth and twitter possibly give me a hand? Thank You
I think this is fixed by twitter by now or you might have missed to provide a default callback url in your application settings, which is required for dynamic callback url to work as mentioned by others above.
Any case, I got this working by passing the oath_callback parameter while retrieving the request token. I am using twitter-async PHP library and had to make a small tweak to make the library pass the callback url.
If you are using twitter-async, the change is below:
modified getRequestToken and getAuthenticateURL functions to take callback url as parameter
public function getRequestToken($callback_url = null)
{
$params = empty($callback_url) ? null : array('oauth_callback'=>$callback_url);
$resp = $this->httpRequest('GET', $this->requestTokenUrl, $params);
return new EpiOAuthResponse($resp);
}
public function getAuthenticateUrl($callback_url = null)
{
$token = $this->getRequestToken($callback_url);
return $this->authenticateUrl . '?oauth_token=' . $token->oauth_token;
}
And pass the callback url from your PHP code.
$twitterObj->getAuthenticateUrl('http://localhost/twitter/confirm.php');
#Ian, twitter now allows 127.0.0.1 and has made some other recent changes.
#jtymann, check my answer here and see if it helps
Twitter oauth_callback parameter being ignored!
GL
jingles
even me to was getting 401 error.. but its resolved..
during registering your application to twitter you need to give callback url...
like http://localhost:8080.
i have done this using java...
so my code is: String CallbackURL="http://localhost:8080/tweetproj/index.jsp";
provider.retrieveRequestToken(consumer,CallbackURL);
where tweetproj is my project name
and index.jsp is just one jsp page...
Hope this may helps u...
After the user authorizes the application on twitter.com and they return to your callback URL you have to exchange the request token for an access token.
Twitter does not honor the oauth_callback parameter and will only use the one specified in the registered application settings.
It also doesn't allow for 127.0.0.1 or localhost names in that callback, so I've setup http://dev.twipler.com which is setup for 127.0.0.1 in DNS so you can safely use;
http://dev.twipler.com/twitter_prompt/index.php

Resources