I'm trying to create my own actions/objects. When checking with the debugger everything is fine, but when testing with my dev account I get this in my callback:
{"data":[]}
Is it good or not because I see anything in my timeline, news feed or ticker.
When testing with a test user I get this error:
{
"error": {
"message": "Call to a member function on a non-object",
"type":"BadMethodCallException"
}
}
What's wrong?
You are supposed to make POST request to facebook api something like below and make sure you have initialised FB:
var opts = {
animal : OBJECT_URL,
access_token: token
};
FB.api('/me/[MY_APP_NAMESPACE]:hunt', 'post', opts, function(response)
{
});
Related
I recently decided to give React Native a shot. I am running into some trouble communicative with Woocommerce using the react-native-woocommerce-api.
When I get all the products, it works great, but when I try to set an attribute to the HTTP call, it fails.
This works:
componentDidMount() {
console.log("Loading products...");
Woocommerce.get("products", {}).then(response => {
console.log(response);
});
}
This doesn't (per_page added):
componentDidMount() {
console.log("Loading products...");
Woocommerce.get("products", {per_page: 3}).then(response => {
console.log(response);
});
}
Error thrown:
Object {
"code": "woocommerce_rest_authentication_error",
"data": Object {
"status": 401,
},
"message": "Invalid signature - the signature doesn't match.",
}
HTTP request that works:
http://www.example.com/wp-json/wc/v3/products?oauth_consumer_key=###&oauth_nonce=###&oauth_signature_method=HMAC-SHA256&oauth_timestamp=1560818379&oauth_version=1.0&oauth_signature=###=&
HTTP request that doesn't work:
http://www.example.com/wp-json/wc/v3/products?oauth_consumer_key=###&oauth_nonce=###&oauth_signature_method=HMAC-SHA256&oauth_timestamp=1560817909&oauth_version=1.0&oauth_signature=###=&per_page=3
I should also add that when adding per_page=3 using Postman, it works. I don't know what difference it makes, the HTTP calls are very similar, it seems that only the uri parameters order are different.
Any help is appreciated! Been stuck on this the whole freaking day. :/
Open react-native-woocommerce-api.js in node_modules\react-native-woocommerce-api\lib\
then goto line 195 (_getOAuth().authorize function) and change:
params.qs = this._getOAuth().authorize({
url: url,
method: method
});
to
params.qs = this._getOAuth().authorize({
url: url + '?' + this.join(data, '&'),
method: method
});
I am confused with shutdown notification mails from Google one of the recent mail mentioned as
projects directly requesting the “plus.me” scope are affected. This scope may have been listed in some emails, even if not directly
requested by your project. We apologize for any confusion caused.
I am using following JS code for login, may I know will it affect anyway due to Google plus api shutdown?
<script async defer src="https://apis.google.com/js/api.js" onload="this.onload=function(){};HandleGoogleApiLibrary()" onreadystatechange="if (this.readyState === 'complete') this.onload()"></script>
<script type="text/javascript">
//google login starts
function HandleGoogleApiLibrary() {
// Load "client" & "auth2" libraries
gapi.load('client:auth2', {
callback: function() {
// Initialize client library
// clientId & scope is provided => automatically initializes auth2 library
gapi.client.init({
apiKey: 'API KEY HERE',
clientId: 'XXXXXXXXXXXXXXXXX.apps.googleusercontent.com',
scope: 'https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email'
}).then(
// On success
function(success) {
// After library is successfully loaded then enable the login button
//CODE AFTER SUCCESS
},
// On error
function(error) {
alert('Error : Failed to Load Library');
}
);
},
onerror: function() {
// Failed to load libraries
}
});
}
// Click on login button
$("#login-button").on('click', function() {
// API call for Google login
gapi.auth2.getAuthInstance().signIn().then(
// On success
function(success) {
// API call to get user information
gapi.client.request({ path: 'https://www.googleapis.com/plus/v1/people/me' }).then(
// On success
function(success) {
console.log(success);
var user_info = JSON.parse(success.body);
//VALIDATION
},
// On error
function(error) {
alert('Error : Failed to login');
}
);
},
// On error
function(error) {
$("#login-button").removeAttr('disabled');
alert('Error : Login Failed');
}
);
});
There is good news and bad news.
The good news is that you're not using any of the plus scopes.
The bad news is that you're using the plus API, which is also being shut down, and which was mentioned in a previous email that should have been sent to you.
Specifically, this chunk of code:
gapi.client.request({ path: 'https://www.googleapis.com/plus/v1/people/me' }).then(
calls the "plus.people.me" API.
Fortunately, you should be able to switch to a different API, such as the "userinfo" API, by changing endpoints to
https://www.googleapis.com/oauth2/v2/userinfo
You may also wish to look into the more modern People API, which works very similarly, and is slightly more complicated, but can provide other profile fields.
We are using Hapi for our simple API. We have out own auth system, so if a user calls our API, but they are not authenticated, I want to send back a 401 HTTP status code.
I saw this post:
https://futurestud.io/tutorials/hapi-how-to-set-response-status-code
which offers this example:
handler: function (request, reply) {
var data = { key: 'value' }
reply(data).code(201)
}
So I started adding in response codes, including codes for our exceptions:
DB.knex.raw(query).then(function(result) {
var dataJson = [];
var responseJson = {};
for(var i in result[0]) {
dataJson.push({
"name": result[0][i]["name"],
"profile_id": result[0][i]["profile_id"],
"profile_type": result[0][i]["profile_type"],
"profile_classification": result[0][i]["profile_classification"],
"permalink": result[0][i]["permalink"],
"location": result[0][i]["location"],
});
}
responseJson["data"] = dataJson;
reply(responseJson).code(200);
})
.catch(function(e) {
console.error(e);
reply(e).code(500);
});
but I immediately got this error:
TypeError: query.then(...).catch(...).code is not a function
So... I guess I can't do this. I am confused by the error, since I am not calling ".code()" on the "catch", I am calling it on the reply().
We are using Boom, does that override the advice in the article? The article talks about Boom, but does not make clear that Boom overrides the earlier advice.
I am an experienced (55+ years) programmer but a total noob in ember and js. I'm trying to get a simple authentication page working using the ember-cli addons ember-cli-simple-auth, ember-cli-simple-auth-oauth2 and cut-and-paste from the simplelabs tutorial.
I get the following in the console:
DEPRECATION: The LoginControllerMixin is deprecated. Use the session's authenticate method directly instead.
and:
DEPRECATION: The AuthenticationControllerMixin is deprecated. Use the session's authenticate method directly instead.
The solution may be trivial, but I have been chasing it for hours and get deep into javascript before reaching a dead-end. The code that is causing these errors is:
import LoginControllerMixin from 'simple-auth/mixins/login-controller-mixin';
export default Ember.Controller.extend(LoginControllerMixin, {
authenticator: 'simple-auth-authenticator:oauth2-password-grant'
});
which invokes the ApplicationControllerMixin somewhere in the bower code.
Before I "re-invent the wheel" by translating some old html/ruby/pascal code into js, can anyone help me "Use the session's authenticate method directly instead."?
Thanks.
I feel you're pain. I spent weeks trying to sort this out. A big part of the problem is that so much has changed in the past couple of years and there are a lot of code examples out there that are outdated or don't work together. It's really difficult to put the various pieces together coherently, and figure out what one does NOT need to do.
That said, please keep in mind that i'm a n00b as well. What i've done seems to work ok but i've no idea whether there's a much better way.
Also, what you're trying to do may not be the same as what i've done. My app authenticates against google (and twitter, fb, etc.) using Simple-Auth-Torii and then exchanges the returned Authenication Code for an Authentication Token. That last part happens on the server. So, after the session authenticates, i then pass the auth code to the server and get back the auth code.
// routes/login.js
import Ember from "ember";
import ENV from "../config/environment";
export default Ember.Route.extend({
setupController: function(controller, model) {
controller.set("errorMessage", null);
},
actions: {
googleLogin: function() {
var _this = this;
// Using "session's authenticate method directly" right here
this.get("session").authenticate("simple-auth-authenticator:torii", "google-oauth2")
.then(function() {
// We're now authenticated. The session should now contain authorization
// code from provider. We now need to exchange that for an auth token.
var secureData = _this.get("session.content.secure");
// call server to initiate token exchange from provider
var exchangeData = {
authorizationCode: secureData.authorizationCode,
redirectUri : secureData.redirectUri,
provider : 'google'
};
// sends ajax request to server, which will in turn call provider
// with authentication code and receive auth token
_this.tokenService.fetch(exchangeData).then(function(response) {
if (response.success) {
_this.set("session.content.secure.access_token", response.data.token);
_this.set("session.content.secure.userData", response.data.user);
// take user somewhere ...
_this.transitionTo("data_sets");
}
else {
// set an error message, log the response to console, whatever, but
// we need to invalidate session because as far as simple-auth
// is concerned we're already authenticated. The following logs the user out.
_this.get("session").invalidate();
}
}, function(error) {
console.log("tokenService.fetch error", error);
_this.get("session").invalidate();
});
}, function(error) {
console.log("simple-auth-authenticator:torii error", error);
_this.get("session").invalidate();
});
},
twitterLogin: function() {
// etc.
}
}
});
Logging a user out also uses the session directly.
{{!templates/application.hbs}}
<ul class="nav navbar-nav navbar-right">
{{#if session.isAuthenticated}}
<li><button {{ action 'invalidateSession' }} class="btn btn-sm">Logout</button></li>
{{/if}}
...
</ul>
// routes/application.js
import Ember from "ember";
import ENV from "../config/environment";
import ApplicationRouteMixin from "simple-auth/mixins/application-route-mixin";
export default Ember.Route.extend(ApplicationRouteMixin, {
actions: {
// action is globally available because in application route
invalidateSession: function() {
// the most basic logout
this.get("session").invalidate();
return;
// If you need to invalidate also on the server do something like:
//
// var _this = this;
// return new Ember.RSVP.Promise(function(resolve, reject) {
// var params = {
// url : ENV.logoutEndpoint,
// type : "POST",
// dataType : "json"
// };
// Ember.$.ajax(params).then(function(response) {
// console.log('session invalidated!');
// console.dir(response);
// _this.get("session").invalidate();
// });
// });
}
}
});
I've met the same deprecation problem. I thinks this snippet for a lovely login controller will do, it's a bit more what you asked, but I hope it is still understandable. I use it with devise, it's almost the same except I use it this way: authenticate('simple-auth-authenticator:devise', credentials)
import Ember from 'ember';
export default Ember.Controller.extend({
actions: {
authenticate: function() {
// identification and password are the names of the input fields in the template
var credentials = this.getProperties('identification', 'password');
if (!credentials.identification || !credentials.password) {
return false;
}
this.get('session').authenticate('simple-auth-authenticator:oauth2-password-grant', credentials).then(function() {
// authentication was successful
}, function(errorMessage) {
// authentication failed
});
}
}
});
I have a small Ember app and adding in authentication at the moment with a simple API in the background:
POST /login
//returns
{
"token": "Much53cr4t"
}
Ember model for login (route setup correctly and calls the endpoint as expected)
App.Login = DS.Model.extend({
username: DS.attr(),
password: DS.attr(),
token: DS.attr()
});
Controller
App.LoginController = Ember.ObjectController.extend({
// Implement your controller here.
actions: {
submit: function() {
var self = this;
var login = self.get('model');
login.set('username', self.get('username'));
login.set('password', self.get('password'));
login.save().then(function (result) {
//do something here?
});
}
}
});
I would like to get the returned token value to be added either to the created model before save, or new one. Whichever is easier. Can't seem to find any other advice other than 'return an id' but that I would consider not the best when it comes to an AUTH API endpoint like this.
So I ended up adding a custom REST adapter for this:
App.LoginAdapter = DS.RESTAdapter.extend({
host: 'http://127.0.0.1',
createRecord: function(store, type, record) {
var p = this._super(store, type, record);
return p.then(function(data){
record.set('token', data['token']);
});
},
});
Still wonder is there an easier, or more 'Ember-way' for doing this?