How to use it...
Opened this issue · 16 comments
Hi mattberg,
your project seems very useful, but I don't have understood how to use it...there is a little tutorial o some instructions somewhere?
thank you
Hi Matthew,
first thanks for this nice add-on. I'll try to explain, how i accomplished it:
first i have copied the auth.php into json-api/controllers to simplify things a bit. next i have copied the get_nonce() from the core controller into auth.php, so i can simply deactivate all functions except those who i need.
then i've setup a simple page (html+js) from where i try to access the json-api via ajax calls.
I've used jQuery and Structure, but you could easily go with jQuery (or Native-Code) too:
Structure.init('App');
//
App.wp_nonce = {};
App.wp_cookie = {};
//
App.registerModule('App.User', {
// her we must our username and password:
getCurrentuserinfo : function(event){
$.ajax({
url:'http://localhost/wordpress_test/api/auth/get_currentuserinfo/',
type:'POST',
//traditional:true,
async:false,
cache:false,
crossDomain:true,
data:{
cookie:App.wp_cookie.cookie
},
dataType:'json',
beforeSend:function(xhr, settings){
trace('getting user info ...')
},
statusCode:{
404:function(){
alert('404 - page not found')
},
403:function(){
trace('403 - forbidden')
}
},
error:function(xhr, status, errorThrown) {
trace(errorThrown)
},
success:function(data, status, xhr){
App.User.wp_user = data.user;
}
});
},
wp_user:{}
}, function (module) {
// module is the registered Module above
});
App.registerModule('App.Auth', {
// her we must our username and password:
generateAuthCookie : function(event){
$.ajax({
url:'http://localhost/wordpress_test/api/auth/generate_auth_cookie/',
type:'POST',
//traditional:true,
async:false,
cache:false,
crossDomain:true,
data:{
nonce:App.wp_nonce.nonce,
username:'tester',
password:'tester'
},
dataType:'json',
beforeSend:function(xhr, settings){
trace('getting auth cookie ...')
},
statusCode:{
404:function(){
alert('404 - page not found')
},
403:function(){
trace('403 - forbidden')
}
},
error:function(xhr, status, errorThrown) {
trace(errorThrown)
},
success:function(data, status, xhr){
App.wp_cookie = data;
App.User.getCurrentuserinfo();
}
});
}
}, function (module) {
// module is the registered Module above
//module.generateAuthCookie(); // initiating
});
App.registerModule('App.Startup', {
// the nonce is free4everyone ;-)
getNonce: function (event) {
// Do something
$.ajax({
url:'http://localhost/wordpress_test/api/auth/get_nonce/', //?dev=1&controller=Posts&method=create_post
type:'POST',
//traditional:true,
async:false,
cache:false,
crossDomain:true,
data:{
controller:'auth',
method:'generate_auth_cookie'
},
dataType:'json',
beforeSend:function(xhr, settings){
trace('getting nonce ...')
},
statusCode:{
404:function(){
alert('404 - page not found')
},
403:function(){
trace('403 - forbidden')
}
},
error:function(xhr, status, errorThrown) {
trace(errorThrown)
},
/*
dataFilter:function(data, type){
trace(data)
},
*/
success:function(data, status, xhr){
App.wp_nonce = data;
App.Auth.generateAuthCookie();
//
}
});
}
}, function (module) {
// module is the registered Module above
module.getNonce(); // initiating
});
Now you have a cookie to authenticate. But beware that you'll need more nonces if you're trying to call other controllers than i did in the example.
Argh! Still not enough information to use this!!!
I can get your code above, to work, with some fixes, but I cannot get any kind of authentication to work when submitting a comment or post!!!
How do you make the JSON API use the cookie?!?!?
I'll see if I can get an example controller for posting using the Auth controller up this week. That will help I think.
Thank you! I was about to try to modify the code to add username/password....
On Oct 23, 2012, at 4:37 PM, Matthew Berg notifications@github.com wrote:
I'll see if I can get an example controller for posting using the Auth controller up this week. That will help I think.
—
Reply to this email directly or view it on GitHub.
...David I. Gross...
Web site: http://www.davidgrossphoto.com/
Email: info@mimetic.com
Fax : +1 (831) 618-8791
Skype : davidgross
CONFIDENTIALITY NOTE:
This e-mail and any attachments or previous e-mail messages attached to it are confidential and may be protected by legal privilege. If you are not the intended recipient, you are hereby notified that any disclosure, distribution, or copying of this message, or any attachment, is prohibited. If you have received this message in error, please notify the original sender immediately by returning it to the sender and delete this copy, along with any attachments, from your computer without reading or saving it in any manner. Thank you for your cooperation.
Hi Matt,
Just wondering if you still intending creating a sample?
I'm creating some custom controllers which will send user specific data, and had intended using your get_currentuserinfo as the basis for my own controller, but would be good to see another example 'from the source'.
Thanks,
Osh
Yes I should be able to add that today. Sorry about that.
Thank you!
On Nov 5, 2012, at 5:54 AM, Matthew Berg notifications@github.com wrote:
Yes I should be able to add that today. Sorry about that.
—
Reply to this email directly or view it on GitHub.
...David I. Gross...
Web site: http://www.davidgrossphoto.com/
Email: info@mimetic.com
Fax : +1 (831) 618-8791
Skype : davidgross
CONFIDENTIALITY NOTE:
This e-mail and any attachments or previous e-mail messages attached to it are confidential and may be protected by legal privilege. If you are not the intended recipient, you are hereby notified that any disclosure, distribution, or copying of this message, or any attachment, is prohibited. If you have received this message in error, please notify the original sender immediately by returning it to the sender and delete this copy, along with any attachments, from your computer without reading or saving it in any manner. Thank you for your cooperation.
OK Check out the latest posts.php file. I updated the original posts.php that comes with the JSON API to use the authentication check from the Auth controller.
I hope I've done this correctly!
I branched wp-json-api, and added my changes.
Now, all actions require authorization.
Please, let me know if I've done the github part right!
Here's the branch:
https://github.com/mimetic/wp-json-api-auth
...David I. Gross...
Web site: http://www.davidgrossphoto.com/
Email: info@mimetic.com
Fax : +1 (831) 618-8791
Skype : davidgross
CONFIDENTIALITY NOTE:
This e-mail and any attachments or previous e-mail messages attached to it are confidential and may be protected by legal privilege. If you are not the intended recipient, you are hereby notified that any disclosure, distribution, or copying of this message, or any attachment, is prohibited. If you have received this message in error, please notify the original sender immediately by returning it to the sender and delete this copy, along with any attachments, from your computer without reading or saving it in any manner. Thank you for your cooperation.
Hey I am working with backbone.js and Looks like there are 3 cookies that need hash to be set properly. Where do you get hash to generate name of the cookie?
The cookie is generated by the Wordpress function "wp_generate_auth_cookie". Make sure to set the type to 'logged_in'.
I have tried for about two days to get this to work.
I am not to sure its the code,
How I am trying to use this
1> Ajax call to get Nonce
$.ajax({
url: "mysiteurl/api/?json=get_nonce&controller=auth&method=generate_auth_cookie",
type: "POST",
cache:false,
async:false,
crossDomain:true,
dataType: "html"
}).done(function(msg){
var contact = JSON.parse(msg);
$('#hv1').val(contact.nonce);
}).fail(function(jqXHR, textStatus) {
alert( "Request failed: " + textStatus );
});
2> Ajax call to generate_auth_cookie with nonce to get cookie
$.ajax({
url: "mysiteurl/api/auth/generate_auth_cookie/",
type: "POST",
cache: false,
async: false,
crossDomain: true,
data: {
username: $('#usernamelogin').val(),
password: $('#passwordlogin').val(),
nonce: $('#hv1').val()
},
dataType: "html"
}).done(function (msg1) {
var cookies = JSON.parse(msg1);
$('#hv2').val(cookies.cookie);
$('#msg').val(cookies.firstname);
}).fail(function (jqXHR, textStatus) {
alert("Request failed: " + textStatus);
});
3> Ajax call to generate_auth_cookie with nonce to get cookie
$.ajax({
url:"mysiteurl/api/auth/get_currentuserinfo/",
type:"POST",
cache: false,
async:false,
crossDomain:true,
data: {username:$('#usernamelogin').val(), password:$('#passwordlogin').val(), nonce:$('#hv1').val(),cookie:$('#hv2').val()},
dataType: "html"
}).done(function(msg2){
var susers = JSON.parse(msg2);
alert(susers);
$.mobile.changePage("#page1", { transition: "slideup"});
}).fail(function(jqXHR, textStatus) {
alert( "Request failed: " + textStatus );
});
I have wrapped each in a separate function and called each in sequence.
If i call the urls and pass parameters in browser I get data. When I call from app / Xcode it never works.
Once I got the nonce value but thats it
I have a plugin in my site to allow cross domain calls. Any help is appreciated
after posting the above code, I ended up getting it working.
One of the functions was named do() , a reserved word. added some return false; and it worked.
Here are the 3 functions that now all work for me
function dop(){
$.ajax({
url:"mysiteurl/api/auth/generate_auth_cookie/",
type:"POST",
cache: false,
async:false,
crossDomain:true,
data: {username:$('#usernamelogin').val(), password:$('#passwordlogin').val(), nonce:$('#hv1').val()},
dataType: "html"
}).done(function(msg1){
var cookies = JSON.parse(msg1);
$('#hv2').val(cookies.cookie);
$('#msg').val(cookies.user.firstname);
return false;
}).fail(function(jqXHR, textStatus) {
alert( "Request failed: " + textStatus );
});
}
function doa(){
$.ajax({
url:"mysiteurl/api/auth/get_currentuserinfo/",
type:"POST",
cache: false,
async:false,
crossDomain:true,
data: {username:$('#usernamelogin').val(), password:$('#passwordlogin').val(), nonce:$('#hv1').val(),cookie:$('#hv2').val()},
dataType: "html"
}).done(function(msg2){
var users = JSON.parse(msg2);
//alert(users);
$.mobile.changePage("#page1", { transition: "slideup"});
return false;
}).fail(function(jqXHR, textStatus) {
alert( "Request failed: " + textStatus );
});
}
function doo(){
$.ajax({
url: "mysiteurl/api/?json=get_nonce&controller=auth&method=generate_auth_cookie",
type: "POST",
cache:false,
async:false,
crossDomain:true,
dataType: "html"
}).done(function(msg){
var contact = JSON.parse(msg);
$('#hv1').val(contact.nonce);
}).fail(function(jqXHR, textStatus) {
alert( "Request failed: " + textStatus );
});
}
then for calling them I attach an event handler to the login button and put it inside a document.ready
$("#btnLogin").on("click", function(){
doo();
dop();
doa();
return false
});
Thanks for making the integration between XCode/PhoneGap & wordpress easier. The authentication was the biggest hurdle for me.
Hi@ all, Hi lismore,
I write my first "app" and have a little problem with your code maybe you can help me. I get only a error -> Request failed: error
hier is my code ->
<html>
<head>
<title>Test</title>
<link rel="stylesheet" href="css/jquery.mobile-1.3.1.min.css" />
<script src="js/jquery-1.9.1.min.js"></script>
<script>
function dop(){
$.ajax({
url:"http://***/api/auth/generate_auth_cookie/",
type:"POST",
cache: false,
async:false,
crossDomain:true,
data: {username:$('#usernamelogin').val(), password:$('#passwordlogin').val(), nonce:$('#hv1').val()},
dataType: "html"
}).done(function(msg1){
var cookies = JSON.parse(msg1);
$('#hv2').val(cookies.cookie);
$('#msg').val(cookies.user.firstname);
return false;
}).fail(function(jqXHR, textStatus) {
alert( "Request failed: " + textStatus );
});
}
function doa(){
$.ajax({
url:"http://***/api/auth/get_currentuserinfo/",
type:"POST",
cache: false,
async:false,
crossDomain:true,
data: {username:$('#usernamelogin').val(), password:$('#passwordlogin').val(), nonce:$('#hv1').val(),cookie:$('#hv2').val()},
dataType: "html"
}).done(function(msg2){
var users = JSON.parse(msg2);
alert(users);
//$.mobile.changePage("#page1", { transition: "slideup"});
return false;
}).fail(function(jqXHR, textStatus) {
alert( "Request failed: " + textStatus );
});
}
function doo(){
$.ajax({
url: "http://***/api/?json=get_nonce&controller=auth&method=generate_auth_cookie",
type: "POST",
cache:false,
async:false,
crossDomain:true,
dataType: "html"
}).done(function(msg){
var contact = JSON.parse(msg);
$('#hv1').val(contact.nonce);
}).fail(function(jqXHR, textStatus) {
alert( "Request failed: " + textStatus );
});
}
// JQ Ready
$(document).ready(function(){
$("#btnLogin").on("click", function(){
doo();
dop();
doa();
return false
});
});
</script>
</head>
<body>
<div id="loginDiv">
<label for="username">Benutzername</label></div></h1>
<input name="usernamelogin" id="usernamelogin" type="text" />
<label for="password">Kennwort</label>
<input name="passwordlogin" id="passwordlogin" type="password" / size=8/>
<input value="Login" id="btnLogin" onclick="login()" type="submit" />
</div>
<div id="hv1"></div>
<div id="hv2"></div>
<div id="msg"></div>
</body>
</html>
Url is marke with ***
Thx and greets from germany :)
Hi all,
I am trying to implement same thing through ajax, curl but I am experience a temporary redirect error. Whenever I hit this /api/?json=get_nonce&controller=auth&method=generate_auth_cookie
it is redirecting to login page. May I know why this is happending