javascript - HybridAuth not working with ajax -
i'm trying implement hybridauth ajax.
code:
php: (will called ajax)
<?php header('content-type: application/json'); $provider = $_get["provider"]; $config = '../libaries/hybridauth/config.php'; require_once( "../libaries/hybridauth/hybrid/auth.php" ); try { $hybridauth = new hybrid_auth($config); $adapter = $hybridauth->authenticate($provider); $userprofile = json_encode($adapter->getuserprofile()); echo $_get['callback'] . '(' . "{$userprofile}" . ')'; } catch (exception $e) { echo "ooophs, got error: " . $e; } ?>
javascript:
socialregister: function () { var self = this; var val = 'provider=' + self.get("provider"); return $.ajax({ type: "get", url: path.urlroot + 'ext/socialregisterandauthentication.inc.php', datatype: "jsonp", data: val }); }
but following error:
xmlhttprequest cannot load https://api.twitter.com/oauth/authenticate?oauth_token=123. no 'access-control-allow-origin' header present on requested resource. origin 'http://localhost' therefore not allowed access.
i know, means, twitter server not allowing origin. there workaround? "one-page" signup ajax possible? (hopefully yes - on quora.com works ;) )
twittersettings:
best fabian
first of - hybridauth , ajax directly not working. have satisfying solution , want share you. here how did it:
javascript:
twitterregister: function () { var self = this; self.popupwindow = window.socialpopupwindow = window.open( path.urlroot + 'ext/socialregisterandauthentication.inc.php?provider=twitter', "hybridauth_social_sing_on", "location=0,status=0,scrollbars=0,width=800,height=500" ); var wintimer = setinterval(function () { if (self.popupwindow.closed !== false) { // !== required compatibility opera clearinterval(wintimer); //now twitter register require(["model/register"], function (registermodel) { var registerm = new registermodel(); var ajaxrequest = registerm.socialregister(); $.when(ajaxrequest).done(function (response) { console.log(response); self.slideoutregister("twitter"); self.twitterobject = response; $('#reg_user').val(response.firstname); }); $.when(ajaxrequest).fail(function () { self.slideoutregister("email"); }); }); } }, 200); },
explanation: function opens new popup-window. user prompted authorize app. setinterval catches close event (fired window when done).
socialregisterandauthentication.inc.php:
<?php session_start(); header('content-type: text/html'); $provider = $_get["provider"]; $config = '../libaries/hybridauth/config.php'; require_once( "../libaries/hybridauth/hybrid/auth.php" ); try { $hybridauth = new hybrid_auth($config); $adapter = $hybridauth->authenticate($provider); $_session["userprofile"] = json_encode($adapter->getuserprofile()); echo "<script type='text/javascript'>"; echo "window.close();"; echo "</script>"; } catch (exception $e) { echo "ooophs, got error: "; } ?>
explanation: closes window when authorization done (this docs of hybridauth). data stored in session can retrieve later per ajax.
getsocialdata.inc.php
<?php session_start(); header('content-type: application/json'); echo $_get['callback'] . '(' . "{$_session["userprofile"]}" . ')'; ?>
explanation: returns stored userprofile.
summary:
open popup-window javascript , let user authorize app. store data in session variable. catch close event of popup-window. make ajax call retrieve stored data (session).
Comments
Post a Comment