with ouath.io how to create the OAuth.create to use as on the callback - facebook

With oauth.io how to make a OAuth.create for twitter
for facebook it is
facebook_auth = OAuth.create("facebook", { oauth_token: "token_here" }, {
"url": "https://graph.facebook.com",
"cors": true,
"query": {
"access_token": "token_here"
}
});
that way you can use it on different parts of your website not just the callback after sign in.
facebook_auth.get('/me?fields=name,email').done(function (data) {
});
otherwise you're stuck only with the callback:
OAuth.callback('facebook', function (err, result) {
result.get('/me?fields=name,email').done(function (data) {
});
});
this is link to their documentation:
https://oauth.io/docs/api

For twitter, it's the same syntaxe than with facebook
var twitterTokens
OAuth.popup('twitter', function(err, res){
twitterTokens = res
})
//then elsewhere
function createTwitter() {
return OAuth.create('twitter', twitterTokens)
}
var twitter = createTwitter()
twitter.get(....)
The 3rd parameter is optional

Related

Axios' then() call to external function cannot get data

methods: {
getHomeInfo() {
axios.get("/js/index.json").then(this.getHomeInfoSucc());
//err:undefined
axios.get("/js/index.json").then(function(res) {
console.log(res.data);
});
//Data available
},
getHomeInfoSucc(res) {
console.log(res);
}
}
I used Axios in Vue to use the anonymous function in Axios's then () to get data, but in then (), I call the function in methods to get the response data undefined
You need a callback:
methods: {
getHomeInfo() {
axios.get("/js/index.json").then((res) => this.getHomeInfoSucc(res));
//err:undefined
axios.get("/js/index.json").then(function(res) {
console.log(res.data);
});
//Data available
},
getHomeInfoSucc(res) {
console.log(res);
}
}

Error API Facebook - Angular 2 and Ionic 2

I'm working with the facebook API and I need to get the profile data to save it in a database. The code returns fine, but when I try to access the data it shows me error because they are null.
This is my code:
this.fb.login(['public_profile', 'user_friends', 'email'])
.then((res: FacebookLoginResponse) => {
token = res.authResponse.accessToken;
userId = res.authResponse.userID;
if (res.status == 'connected') {
this.fb.api('/' + res.authResponse.userID + '?fields=id,first_name,last_name,gender, email,birthday', [],
function onSuccess(result) {
var name = JSON.stringify(result.first_name);
this.register(name);
},
function onError(error) {
console.log(error);
});
} else {
console.log('Not logged in');
}
}).catch(e => console.log('Error logging into Facebook', e));
When I try to call the register method it generates error because the method does not exist, but this is not true. I also tried to save the result of name to a global variable but when it is to be assigned it shows that it can not assign the variable, ie as if the global variable was not defined.
The solution was to change
function onSuccess(result) {
var name = JSON.stringify(result.first_name);
this.register(name);
},
function onError(error) {
console.log(error);
});
for
(result) => {
var name = JSON.stringify(result.first_name);
this.register(name);
},
(error) => {
console.log(error);
});

Push notifications with Ionic (1.3)?

I am trying to set up push notifications with Ionic, mainly by following the official docs: https://docs.ionic.io/services/push/.
However I'm not sure where to call certain bits of code, e.g. $ionicPush.register(). Has anyone come across a recent / up-to-date tutorial that's helpful with this? Tutorials that I've come across use deprecated bits of code, e.g. ionic add.
If you are using Ionic User as well here is the flow I use. These are various functions
$scope.signupForm={};//user object
$scope.beginSignUp=function(){
//sign up
var details = {
'email': $scope.signupForm.email,
'password': $scope.signupForm.pass,
'details':{
'name':$scope.signupForm.dn,
'image':$scope.signupForm.dp
}
}
Ionic.Auth.signup(details).then(signupSuccess, function(err){
});
}
$scope.signupSuccess=function(){
console.log("ionic sign up sucess, now login the user");
var authProvider = 'basic';
var authSettings = { 'remember': true };
var loginDetails = {
'email': $scope.signupForm.email,
'password': $scope.signupForm.pass
};
Ionic.Auth.login(authProvider, authSettings, loginDetails)
.then(authSuccess, function(err){
});
}
function authSuccess(){
console.log("Ionic Login Success ");
var ionic_user = Ionic.User.current();
ionic_user.details.name =$scope.signupForm.dn;
ionic_user.details.image = $scope.signupForm.dp;
ionic_user.save();
registerPush();
$ionicHistory.nextViewOptions({
disableBack: true
});
$state.go('home', {}, {reload: true});
}
function registerPush(){
console.log("Calling Register Push");
$ionicPush.init({
"debug": true,
"onNotification": function(notification) {
var payload = notification.payload;
console.log(notification, payload);
},
"onRegister": function(data) {
console.log(data.token);
Ionic.User.current();
$ionicPush.saveToken(data.token);
//unregister after checking
},
"pluginConfig": {
"ios": {
"badge": true,
"sound": true
},
"android": {
"iconColor": "#FA2B2E",
"senderID": "GCM Project No.",
"icon":"notification"
}
}
});
$ionicPush.register();
}
Use the model signupForm for storing user's email, password etc etc and call beginSignUp() on a button click. Or if it is a login call signupSuccess()

How to use the login credentials with php in ionic project

I want to authenticate the user_name and password field. the user_name and password field is stored in database with php. how to get the data from the server in ionic project.
Thanks in advance.
You can create a service script that can send post data to PHP and receive a JSON response.
Post data should be sent as an object containing element name and values in the following format:
var myObj = {username: 'username', password:'password'};
Below is a service example:
yourApp.service('YourService', function ($q, $http) {
return {
login: function (data) {
var deferred = $q.defer(),
promise = deferred.promise;
$http({
url: 'http://www.example.com/yourPHPScript.php',
method: "POST",
data: data,
headers: {'Content-Type': 'application/json'}
})
.then(function (response) {
if (response.data.error.code === "000") {
deferred.resolve(response.data.appointments);
} else {
deferred.reject(response.data);
}
}, function (error) {
deferred.reject(error);
});
promise.success = function (fn) {
promise.then(fn);
return promise;
};
promise.error = function (fn) {
promise.then(null, fn);
return promise;
};
return promise;
}
};
});
From your login controller you call the following code to use the service (make sure you add the name of the service to your controller declaration)
YourService.login(loginData)
.then(function (data) {
// on success do sthg
}, function (data) {
//log in failed
// show error msg
});

Angularjs RESTul Resource Request

I am trying to make the request
....port/trimService/fragments/?fragment_name=:fragmentName
However if I try to make the "?fragment_name" a parameter, it breaks. As I am going to have more requests, my action with change so I cannot leave it in the url portion of the resource.
angular.module(foo).factory('FragmentService', ['$resource',
function ($resource)
{
var FragmentService = $resource('.../fragments/:action:fragmentName',
{},
{
'getFragments':
{
method: 'GET',
isArray: true,
params:
{
fragmentName: "#fragmentName",
action: "?fragment_name="
}
}
});
return FragmentService;
}
]);
As of right now, I have no idea what my URL is actually outputting.
EDIT: I changed my resource as /u/akonsu had mentioned below. I also added my controller as it is still not working correctly.
angular.module(foo).factory('FragmentService', ['$resource',
function ($resource)
{
var FragmentService = $resource('.../fragments/',
{},
{
'getFragments':
{
method: 'GET',
isArray: true,
params:
{
fragmentName: "#fragmentName",
}
}
});
return FragmentService;
}
]);
angular.module(foo).controller('FragmentController', ['$scope', 'FragmentService',
function ($scope, FragmentService)
{
$scope.fragmentQuery = {
fragmentName: 'a',
};
$scope.fragmentQuery.execute = function ()
{
if ($scope.fragmentQuery.fragmentName == '')
{
$scope.fragments = {};
}
else
{
$scope.fragments = FragmentService.getFragments(
{
fragmentName: $scope.fragmentQuery.fragmentName,
});
}
};
$scope.fragmentQuery.execute();
}
]);
Try omitting the query string altogether in the resource URL and just supply your fragmentName as a parameter to the action call. It should add it to the query string if it is not in the list of URL parameters.
$resource(".../port/trimService/fragments/").get({fragmentName: 'blah'})