All my yammer requests come back Acess Denied - yammer

I have tried a host of REST endpoints in my app and all of them return access denied. I am baffled by this. I have double checked the app id and I know I am getting logged in. Here is the complete source:
<body>
<button onclick='post()'>Post on Yammer!</button>
<button onclick='loginStatus()'>Login Status</button>
<button onclick='postActivity()'>Post Activity</button>
<button onclick='getSuggestions()'>Get Suggestions</button>
<button onclick='getUser()'>Get User</button>
<button onclick='getMessages()'>Get Messages</button>
<button onclick='search()'>Search</button>`enter code here`
<button onclick='getGroups()'>Groups</button>
<script type="text/javascript">
//<![CDATA[
function post() {
yam.getLoginStatus(function (response) {
if (response.authResponse) {
alert(1);
yam.request({
url: "https://www.yammer.com/api/v1/messages.json",
method: "POST",
data: { "body": "HelloTest" },
success: function (msg) { alert("Post was Successful!: " + msg); },
error: function (msg) { Console.dir(msg);}
});
} else {
alert(2);
yam.login(function (response) {
if (!response.authResponse) {
yam.request({
url: "https://www.yammer.com/api/v1/messages.json",
method: "POST",
data: { "body": "HelloTest" },
success: function (msg) { alert("Post was Successful!: " + msg); },
error: function (msg) { Console.dir(msg); }
});
}
});
}
});
}
function loginStatus() {
yam.getLoginStatus(
function (response) {
if (response.authResponse) {
alert("logged in");
console.dir(response); //print user information to the console
}
else { //authResponse = false if the user is not logged in, or is logged in but hasn't authorized your app yet
alert("logged out");
}
}
);
}
function postActivity() {
yam.request({
url: "https://www.yammer.com/api/v1/activity.json",
method: "POST",
data: {
"activity": {
"actor": {
"name": "Jeff Kirkham",
"email": "garthf#retailv3dev2.onmicrosoft.com"
},
"action": "create",
"object": {
"url": "https://www.yammer.com/retailv3dev2.onmicrosoft.com",
"title": "Lunch Meeting"
},
"message": "Hey, let’s get sushi!",
"users": [
{
"name": "Jeff Kirkham",
"email": "garthf#retailv3dev2.onmicrosoft.com"
}
]
}
},
success: function (msg) { alert("Post was Successful!: " + msg); },
error: function (msg) { console.dir(msg); }
})
}
function getUser() {
yam.request({
url: "https://www.yammer.com/api/v1/users/current.json",
method: "GET",
success: function (msg) { alert("Get was Successful!: " + msg); },
error: function (msg) { console.dir(msg); }
})
}
function getMessages()
{
yam.request({
url: "https://www.yammer.com/api/v1/messages.json",
method: "GET",
success: function (msg) { alert("Get was Successful!: " + msg); },
error: function (msg) { console.dir(msg); }
})
}
function search()
{
yam.request({
url: "https://www.yammer.com/api/v1/search.json",
method: "GET",
search: "Marketing",
success: function (msg) { alert("Get was Successful!: " + msg); },
error: function (msg) { console.dir(msg); }
})
}
function getGroups() {
yam.platform.request({
url: "groups.json?mine=1",
method: "GET",
data: {},
success: function (msg) { alert("Get was Successful!: " + msg); },
error: function (msg) { console.dir(msg); }
})
};
function getSuggestions() {
yam.getLoginStatus(
function (response) {
if (response.authResponse) {
console.log("logged in");
yam.platform.request({
url: "suggestions.json", //this is one of many REST endpoints that are available
method: "GET",
data: { //use the data object literal to specify parameters, as documented in the REST API section of this developer site
"letter": "a",
"page": "2",
},
success: function (user) { //print message response information to the console
alert("The request was successful.");
console.dir(user);
},
error: function (user) {
alert("There was an error with the request.");
console.dir(user);
}
});
}
else {
alert("not logged in")
}
}
);
}
//]]>
</script>
<script data-app-id="gYLlvylCHqAYNiwAa40AHw" src="https://c64.assets-yammer.com/assets/platform_js_sdk.js" type="text/javascript">
</script>
</body>

Note: Instead of plastering a really long script for everyone else to debug, it would be more helpful if you narrowed the scope of your script down to a single function that was not working as expected.
One point I noticed is that you are calling www.yammer.com/api/v1/.
We've restricted JS SDK calls to www.yammer.com/api/v1 endpoints due to CORS. Instead, omit the hostname and just focus on the endpoint like you've done in the suggestions.json example.

Related

Datatables.net is not loading any data using AJAX

I am trying to get some data from an API and load into Datatables (datatable.net). I have completed the code as per guide and it doesn't load at all.
AJAX completes the request successfully.
Code below:
function loadTable(api) {
$.ajax({
url: api,
type: 'GET',
dataType: 'json',
"dataSrc": "",
beforeSend: function () {
console.log('\"Data collection\" query operation triggered')
$(".overlay").show(); //We are showing loading spinner here
},
headers: { Authorization: 'Basic XXXX },
success: function (data, textStatus, xhr) {
//Populate Table
$('#datatable').DataTable({
"columns": [
{ "data": "DataObjectName" },
{ "data": "SourceObject" },
{ "data": "IndnexType" },
{ "data": "ObjectType" },
{ "data": "FeedType" },
{ "data": "BusinessUnit" },
{ "data": "VersionId" },
{ "data": "Unit" },
{ "data": "Unit" },
],
"bDestroy": true
})
console.log('\"Data collection\" query operation succesfully completed');
},
error: function (xhr, textStatus, errorThrown) {
console.log('\"Data collection\" query operation failed. Error : ' + errorThrown);
showNotification('top', 'right', 'Error while performing ' + operation + '. Error : ' + errorThrown, 'danger');
},
complete: function () {
$(".overlay").hide(); //We are showing loading spinner here
console.log('\"Data collection\" query operation task completed');
}
});
}

parsererror SyntaxError: Unexpected token < in JSON at position 0

submitForm = function (nid, title) {
var form = $('#candidature')[0];
var formData = new FormData(form)
var btn = document.querySelector('#candidature button')
formData.append('listfields', JSON.stringify(fieldsname))
formData.append('nid', nid)
formData.append('title', title)
formData.append('ntype', 'recrutement_offre')
console.log(global_json.baseUrl)
$.ajax({
url: global_json.baseUrl + "/send-form",
type: "post",
data: formData,
processData: false,
contentType: false,
dataType: "json",
beforeSend: function() {
btn.innerHTML = '<i class="fa fa-circle-o-notch fa-spin" style="font-size:12px !important"></i> Veuillez patienter...'
},
success: function (response) {
if (response.success) {
$('form#candidature').hide();
//console.log('success');
}
showMessage(response, 'candidature');
},
complete: function (response) {
btn.innerText = 'Envoyer'
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown)
//$('form#candidature').hide();
}
})
}
var fieldsname = {
"name": "field_field_name_rec",
"type": "field_field_type_rec",
"rules": "field_rules_rec",
"values": "field_values_rec"
}
$('#formModal').on('shown.bs.modal', function(event) {

CSRF token error? on laravel Symfony\\Component\\HttpKernel\\Exception\\HttpException

first was ok. second got error
I use the ajax function on javascript page in laravel
If I initiate the function once it work well
But when I start the function 2 or 3 times in short time I got the error
"exception": "Symfony\\Component\\HttpKernel\\Exception\\HttpException",
"file": "D:\\AppServ\\www\\come\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Exceptions\\Handler.php",
I search the error message . The result is the csfr issue.
But how can I fix the error?
I have already have the
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
The question is not on the first time . It's on the second or third times.
Code
$('.findNews_geography').autocomplete({
source: function(request, response) {
var findtable=$('.findtable_num').val();
var terms=request.term;
console.log("findtable="+findtable+";term="+terms);
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
url: "findNews_geography",
dataType: "json",
type: "post",
data: {
findtable : findtable,
term : terms,
},
error: function(xhr, ajaxOptions, thrownError) {
console.log("findNews_geography ajax error="+xhr.responseText);
console.log("findNews_geography xhr.status="+xhr.status+";thrownError="+thrownError);
},
success: function(data) {
console.log("see request="+data);
response( $.map( data, function( item ) {
return {
label: item.place,
}
}));
} //success end
}); //ajax end
}, //source end
minLength: 0,
}); //autocomplete end
$(".findNews_geography").focus(function () {
//if (this.value == "") {
console.log("findNews_geography get focus");
if($('.findtable_num').val()){
$(this).autocomplete("search");
}// };
});
$('.findNews_geography').autocomplete({
source: function(request, response) {
var findtable=$('.findtable_num').val();
var terms=request.term;
console.log("findtable="+findtable+";term="+terms);
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
url: "findNews_geography",
dataType: "json",
type: "post",
data: {
findtable : findtable,
term : terms,
_token: $('meta[name="csrf-token"]').attr('content')
},
error: function(xhr, ajaxOptions, thrownError) {
console.log("findNews_geography ajax error="+xhr.responseText);
console.log("findNews_geography xhr.status="+xhr.status+";thrownError="+thrownError);
},
success: function(data) {
console.log("see request="+data);
response( $.map( data, function( item ) {
return {
label: item.place,
}
}));
} //success end
}); //ajax end
}, //source end
minLength: 0,
}); //autocomplete end
Try to send the csrf token in your ajax request as data
data: {
findtable : findtable,
term : terms,
_token: $('meta[name="csrf-token"]').attr('content')
},
Hope this helps

Mvc autocomplete ajax

Try to:
$(document).ready(function () {
$('#cityName').autocomplete({
source: function(request,response) {
$.ajax({
type: 'POST',
url: '#Url.Action("Search", "City")',
dataType: 'json',
data: { name: request.term } ,
success: function (data) {
response($.map(data, function (item) {
alert(JSON.stringify(data));
alert(JSON.stringify(item.name));
return {
name: item.name,
label: item.name
}
}));
}
})
},
messages: {
noResults: "", results: ""
}
})
})
In alert(JSON.stringify(data)) got this: {"items":["Boston","Berlin"]}.
In alert(JSON.stringify(item.name)) got this: undefined.
Question: how do it (item.name) works?
You have to just return array of strings:
$(document).ready(function () {
$('#cityName').autocomplete({
source: function(request,response) {
$.ajax({
type: 'POST',
url: '#Url.Action("Search", "City")',
dataType: 'json',
data: { name: request.term } ,
success: function (data) {
response(data.items);
}
})
},
messages: {
noResults: "", results: ""
}
})
})

Assign page size value to kendo grid from code

code:
var dataSource = new kendo.data.DataSource({
transport: {
read: {
url: "http://search.twitter.com/search.json",
dataType: "jsonp",
data: {
q: "kendoui"
}
}
},
schema: {
data: "results",
total: function(response) {
return response.results.length;
}
},
pageSize: 4
});
here i have to set the page size 4 from client side
public JsonResult GetSettings()
{
return Json(new { count = Service.GetSettings<UserSetting>(AuthenticatedUser) }, JsonRequestBehavior.AllowGet);
}
var settingsDataSource = new kendo.data.DataSource({
transport: {
read: {
url: '#Url.Action("GetSetting")',
dataType: "json",
type: "GET"
}
},
schema: {
parse: function (data) {
resultCount = data.count;
return data;
}
},
change: function () {
Grid();
}
});
settingsDataSource.read();
function Grid() {
mainGridDataSource = new kendo.data.DataSource({
transport: {
read: {
url: '#Url.Action("GetDetails")',
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8"
},
parameterMap: function (options) {
return JSON.stringify({ filter: options, isPrimary: options.isPrimary });
}
},
schema: {
model: {
fields: {
Status: { type: "string" },
Name: { type: "string" }
}
},
data: function (data) {
return data.data;
},
total: function (data) {
return data.totalCount;
}
},
pageSize: resultCount,
serverFiltering: true,
serverPaging: true
});

Resources