Wordpress Rest api and ionic - ionic-framework

Is it possible for an Ionic application to fetch data from multiple WordPress REST APIs based on certain conditions like user login? Can the Ionic app fetch data from multiple headless WordPress REST APIs and display them on the frontend?

Yes it is possible to make rest api using wordpress
Create Custom Plugin for you rest api
Make a file and add this code in plugin and then activate the plugin from wp-admin
/*
Plugin URI: http://www.test.com/
Description: Develope Rest API for mobile interaction
Author: test
Version: 1.1.1
Author URI: http://www.test.com/
Text Domain: test_custom
Domain Path: /languages
Network: true
*/
Now add rest routes in it by adding code, It will create post method
add_action('rest_api_init', function() {
register_rest_route('julisa/v1', 'login', array(
'methods' => WP_REST_SERVER::CREATABLE,
'callback' => 'test',
'permission_callback' => function () {
return true;
}
));
});
Now make function for your route
public function test(WP_REST_Request $request){
return "test";
}
Now type your business login and enjoy it.

Related

Is there a way to get a users Bitmoji using Access tokens from Snapkit login web api?

I am attempting to use the snapkit login web api for a hybrid application. I have successfully been able to intercept the access token in the redirectURL. I was wondering if there was a way to get the users Bitmoji using this access_token and either the functions found in login.js or an http get call?
Api docs: https://docs.snapchat.com/docs/login-kit/#web
currently I have the access_token in a deeplinking function on my app.component.ts . I have attempted to push to a new page with the navController and passing in the access_token as a parameter, but this doesn't help when attempting to get the users information.
Thanks in advance for your help.
Here is the Deeplinking where I intercept the access_token using myapp://settings-set/ as the URL redirect and attempt to push a new page with the matching url.
platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
statusBar.styleDefault();
splashScreen.hide();
this.deeplinks.routeWithNavController(this.nav,{
'/settings-set/:token': SettingsSetPage
}).subscribe((match) => {
// match.$route - the route we matched, which is the matched entry from the arguments to route()
// match.$args - the args passed in the link
// match.$link - the full link data
this.nav.push(SettingsSetPage, {
args: match
});
console.log('Successfully matched route', match.$args);
},
(nomatch) => {
// nomatch.$link - the full link data
console.error('Got a deeplink that didn\'t match', nomatch);
});
});
}
In the setting-set page I recieve the parameter using:
this.args = navParams.get('args');
console.log("this is args", JSON.stringify(this.args));
but don't know how to use the information to get the users information
The Bitmoji API can be very confusing at times. I suggest using Passport, a Node JS tool for OAuth, along with the Ionic framework. Snapchat has a guide that explains how to grab specific fields, such as user name and Bitmoji avatar, from a user's Snapchat profile using passport. You can follow this tutorial to learn how to integrate Node JS into your existing ionic app.
So in conclusion, try following these steps:
Integrate Node JS into your existing ionic app
Install Passport and follow Snapchat's guide for obtaining specific fields from the user's profile
Yes, like Mora said you can use passport which will make your life easier. We also have a sample passport app running here:
From the context you provided it seems like you have generated the code and not the access_token. After you get the code from the redirect url, you need to use the code to generate the access token. Check section 2.5 here.
Once you have the access token you can use that to request information. The crux of this lies in setting the "scope" correctly. To get the Bitmoji avatar make sure you set your scope to this at the very least:
var scope = ['https://auth.snapchat.com/oauth2/api/user.bitmoji.avatar'];
Hope this helps!

SAP Fiori Get logged in user details in UI5 application

I've a SAP Fiori application and I need to get the current logged in user details.
I've searched web but unable to find a solution.
is there any way to get current logged in user details from launchpad.
There is a UserInfo service from the FLP shell which can be retrieved like this:
{ // In Controller
doSomethingUserDetails: async function() {
const oUserInfo = await this.getUserInfoService();
const sUserId = oUserInfo.getId(); // And in SAPUI5 1.86, those became public: .getEmail(), .getFirstName(), .getLastName(), .getFullName(), ...
// ...
},
getUserInfoService: function() {
return new Promise(resolve => sap.ui.require([
"sap/ushell/library"
], oSapUshellLib => {
const oContainer = oSapUshellLib.Container;
const pService = oContainer.getServiceAsync("UserInfo"); // .getService is deprecated!
resolve(pService);
}));
},
}
To align with the current best practices, avoid calling sap.ushell.Container.getService directly!
getService is deprecated. Use getServiceAsync instead.
Require the library instead of referencing the Container via global namespace (sap.ushell.*) as shown above.
Alternatively, information about the the current user can be also retrieved via the user API service exposed by the application router from the SAP Business Technology Platform (SAP BTP).
* In case the app is deployed to the old Neo environment, see the previous edit.
The User-ID can be retrieved from SDK.
Please refer to class sap.ushell.services.UserInfo
Try this:
new sap.ushell.services.UserInfo().getId()
If you want to access the user detail when you are running your application in launchpad. then you can retrieve current user detail by adding following code snippet:
var userInfo = sap.ushell.Container.getService("UserInfo");
var email = userInfo.getEmail();
Then further detail about the user can be retrieved like email and fullname. See the API here.

Facebook Messenger Platform Setup

I have a general question regarding the setup for a "bot" in the Facebook Messenger Platform.
If I understand the architecture right, I can create an App as a developer add the Messenger function and associate 1 Page with the Messenger function.
Does this mean I need an app for each page ?
Or could I crete a "bot backend" serving multiple / different pages from different users ?
Yes, you can have one robot serving multiple pages. You just have to set <token> for different pages in API call, here is setup for a page. From documentation:
Graph API requires Page access tokens to manage Facebook Pages. They are unique to each Page, admin and app and have an expiration time.
Fritak is correct. You can use one app for multiple pages. For each page you will have to subscribe the app to that page and generate a page access token specifically for that page. At your webhook, you'll have to distinguish the callbacks for the specific page.
When you receive a request, you need to map the incoming page id to the access token as described in this answer: How can I use the same bot on multiple facebook pages using bot framework
app.post('/webhook', (req, res) => {
const data = req.body
// Make sure this is a page subscription
if (data.object === 'page') {
// Iterate over each entry
data.entry.forEach((pageEntry) => {
// get the pageId
const pageId = pageEntry.id
...
const accessTokens = {
myPageId1: 'myPageAccessToken1',
myPageId2: 'myPageAccessToken2',
}
const callSendAPI = (pageId, messageData) =>
rp({
uri: 'https://graph.facebook.com/v2.8/me/messages',
qs: { access_token: accessTokens[pageId] },
method: 'POST',
body: messageData,
json: true,
})

How to integrate Facebook SDK into node js?

One benefit of node js that we can use the ready made huge base of javascript libraries? So, I was wondering how to integrate the Facebook sdk at http://connect.facebook.net/en_US/sdk.js into my node project?
I am a bit new to Node so the question might be naive..
You cannot integrate the Facebook SDK directly. There are some libraries at http://npmjs.org which deal with Facebook connectivity, but most of them are quite old.
If you're just looking for Facebook Login integration, https://www.npmjs.org/package/passport-facebook could be a starting point.
I ended up developing my of "library", which is not too complicated because the Facebook Graph API can be used via standard HTTP requests.
Try this .
First do npm install facebook-nodejs-sdk
And then
var connect = require('connect'),
fbsdk = require('facebook-sdk');
connect()
.use(connect.cookieParser())
.use(connect.bodyParser())
.use(fbsdk.facebook({
appId : 'YOUR APP ID',
secret : 'YOUR API SECRET'
}))
.use(function(req, res, next) {
if (req.facebook.getSession()) {
res.end('Logout');
// get my graph api information
req.facebook.api('/profile', function(me) {
console.log(me);
});
} else {
res.end('Login');
}
})
.listen(9000);

REST web service Basic authentication + Client Facebook app authentication = double authentication?

I have built a web service based on a REST design. Of course some of the operations available on resources require authentication (delete a user for example). I use Basic authentication and so far everything is fine.
I have built a client to consume the web service : a set of Ajax function. Again, everything is fine (also for the Basic authentication).
I want now to create a whole web app that will use the set of Ajax function above to interact with the web service. But, to enhance the user experience, some of the web app functions will require Facebook authentication.
So here is my problem. The web app will require username and password to call the web service via the Basic authentication. But it will also require Facebook credentials to use Facebook API and the user will have to log in twice. Moreover, every time I will have to check if the Facebook user (currently logged in Facebook) corresponds to the user of the web service and it is quite troublesome.
Does anyone have an idea to simplify the process ?
It's a bit related to that post authentication-scheme-for-multi-tiered-web-application-utilizing-rest-api but I did not find any answer I could understand.
In such scenarios, I use only Facebook authentication. If user is logged into Facebook by JS SDK, you can simply get accessToken by FB.getAuthResponse().accessToken. Then, you can pass it into webservice and use it to authenticate on server side.
First, client side authentication with JS SDK:
/* assumed, that you alredy called FB.login() and stuff */
var accessToken = FB.getAuthResponse().accessToken;
$.ajax({
'url' : 'rest.php',
'type' : 'get',
'dataType' : 'json',
'data' : { accessToken : accessToken },
'success' : function(response) {
/* some fancy code, blah blah blah */
}
});
I use PHP SDK, so I'll show example in PHP.
<?php
require 'facebook.php';
$accessToken = $_GET['accessToken'];
$facebook = new Facebook(array(
'appId' => 'xxxxx',
'secret' => 'xxxxx'
));
$facebook->setAccessToken($accessToken);
if ($facebook->getUser()) {
// yaay, user is authenticated
echo json_encode($mySuperDuperSecretContentForLoggedUserOnly);
} else {
// authentication failed
echo json_encode(null);
}
If your main design uses basic authentication, but you want on some cases to be able to connect with facebook, then I suggest that you use an adapter/bridge on your server that will be able to take the facebook auth data and then handle the basic authentication itself.
That way the facebook users won't have to go through 2 authentication processes, and you don't break anything in your original flow.
It should not be hard to implement, when a user signs up just associate his fb account with a user in your system, then when he logs in with facebook take his id and log him into the system using the basic authentication.
One thing though, if you want to implement the facebook authentication using client side then you should send the auth data in https.