(#3) App must be on whitelist facebook - javascript

I'm trying to post a messsage to a wall using FB.api
My perms are: 'email, read_stream, publish_stream' and my code is:
FB.getLoginStatus(function(response){
if(response.session) {
var accessToken = response.session.access_token;
var tokenUrl = "https://graph.facebook.com/me/feed?access_token=" + accessToken + "&callback=?";
var shareUserId = document.getElementById("shareHidden").value;
var shareTxtAreaMsg = document.getElementById("shareTxtArea").value;
console.log("friends user Id: " + shareUserId + " & " + "message: " + shareTxtAreaMsg);
var data = {
message: "shareTxtAreaMsg",
display: 'iframe',
caption: "Caption",
name: "Name",
picture: 'http://someDomain.com/Dev/img/share-force-wall-img.jpg',
link: "http://www.facebook.com/pages/someapp/XXXXXXXXXXX?sk=app_XXXXXXXXXXXXXX", // Go here if user click the picture
description: "Description field",
actions: [{ name: 'action_links text!', link: 'some link' }],
}
console.log(accessToken);
FB.api(tokenUrl, 'post', data, function(response){
if (response)
{
//console.log(response);
if (response.error)
{
console.log(response.error.message);
}
else
{
if (response.id)
console.log("Posted as post_id "+response.id);
else if (response.post_id)
console.log("Posted as post_id "+response.post_id);
else
console.log("Unknown Error");
}
}
});
}
});
When when try to post the message I'm getting a "(#3) App must be on whitelist" returned. Why is this happening?

Try FB.api('/me/feed'... instead of tokenUrl because FB.api will automatically add the full url prefix.

Related

Angular display a single record from json

I've just started getting into using Angular and having an issue displaying a single record that is being returned from using $http (get). I'm getting the data back correctly. This is the html I've got....
<div ng-controller="UserDataController as udCtrl">
Name: {{udCtrl.user.name}}
</div>
<div id="debug" style="margin-top:24px;border:solid 1px #900;background:#efefef;min-height:100px"></div>
have also tried and a couple of other variations...
Name: {{udCtrl.name}}
Javascript:
(function() {
var app = angular.module('rtcTimesheet', []);
var servicePath="/angular/";
$("#debug").append("Starting...<br/>");
app.controller("UserDataController",["$http",function($http){
var user=this;
user=[];
$http({
method: 'GET',
url: servicePath+'login.php',
params: {
un: "username",
pwd: "123456789"
}
}).then(function(response){
if(response.data.hasOwnProperty("HasError")) {
$("#debug").append("ERROR: " + response.data.ErrorMessage);
} else {
$("#debug").append("Data: " + JSON.stringify(response.data));
user=response.data;
}
},function (err){
alert("ERROR: "+err.status); //data, status, headers, config, statusText
});
}]);
app.controller("UserTest",function(){
this.user=users;
});
var users = {
id: '1',
name: 'Joe Bloggs'
};
})();
This is what is returned in JSON format and I can see this in the little debug area I created.
{"data":{"id":"1","name":"Joe Bloggs"}
if I change the html to use the code below it works.
<div ng-controller="UserTest as udCtrl">
Name: {{udCtrl.user.name}}
</div>
I just cannot see where I'm going wrong and why it will not display the returned name.
Define the user variable on $scope and access it with $scope.user. You are having referance problem.
Example
//Define user variable like this.
$scope.user = {};
//On response -->
}).then(function(response){
if(response.data.hasOwnProperty("HasError")) {
$("#debug").append("ERROR: " + response.data.ErrorMessage);
} else {
$("#debug").append("Data: " + JSON.stringify(response.data));
$scope.user=response.data;
}
}
EDIT
If you want to use udCtrl referance define variable under thisvariable on controller.
//Define user variable like this.
var ctrl = this;
ctrl.user = {};
//On response -->
}).then(function(response){
if(response.data.hasOwnProperty("HasError")) {
$("#debug").append("ERROR: " + response.data.ErrorMessage);
} else {
$("#debug").append("Data: " + JSON.stringify(response.data));
ctrl.user=response.data;
}
}
EDIT 2 FOR ABSOLUTE ANSWER
(function() {
var app = angular.module('rtcTimesheet', []);
var servicePath="/angular/";
$("#debug").append("Starting...<br/>");
app.controller("UserDataController",["$http",function($http){
var udCtrl=this;
udCtrl.user=[];
$http({
method: 'GET',
url: servicePath+'login.php',
params: {
un: "username",
pwd: "123456789"
}
}).then(function(response){
if(response.data.hasOwnProperty("HasError")) {
$("#debug").append("ERROR: " + response.data.ErrorMessage);
} else {
$("#debug").append("Data: " + JSON.stringify(response.data));
udCtrl.user=response.data;
}
},function (err){
alert("ERROR: "+err.status); //data, status, headers, config, statusText
});
}]);
app.controller("UserTest",function(){
this.user=users;
});
var users = {
id: '1',
name: 'Joe Bloggs'
};
})();

Assigning global variable to following Javascript

How can we make the var modal = $("#modal"); a global variable?
Where we have the modal variable in the following code, we need it to work, it currently doesn't,
var content = "Hello " + name + ", You have signed " + modal + " up to XYZ";
Many, many thanks!
$(document).ready(function() {
$('#calendar').fullCalendar({
header: {
left: '',
center: 'title',
right: 'prev,next today'
},
defaultDate: '2016-03-15',
events: [
{
title: 'Event',
start: '2016-03-26T11:00:00',
end: '2016-03-26T12:00:00',
},
],
eventClick: function(event) {
console.log(event)
// alert(event.start.format('MMMM Do YYYY'))
start = event.start.format('MMMM Do YYYY'),
end = event.end.format('MMMM Do YYYY'),
html = '<p>Starts: ' + start + '<p>';
html += '<p>Ends: ' + end + '<p>';
var modal = $("#modal");
modal.find(".modal-title").html(event.title);
modal.find('.modal-body').html(html)
modal.modal();
}
})
$("#contact_form").submit(function() {
var email = $("#email").val(); // get email field value
var name = $("#name").val(); // get name field value
var msg = $("#msg").val(); // get message field value
var content = "Hello " + name + ", You have signed " + modal + " up to XYZ";
$.ajax({
type: "POST",
url: "https://mandrillapp.com/api/1.0/messages/send.json",
data: {
'key': 'api',
'message': {
'from_email': "email",
'text': "Hello ",
'from_name': "name",
'headers': {
'Reply-To': "email"
},
'subject': 'Confirmation - Sign Up',
'text': content,
'to': [{
'email': email,
'name': name,
'type': 'to'
}]
}
}
})
.done(function(response) {
alert('You have been signed up. Thank you!'); // show success message
$("#name").val(''); // reset field after successful submission
$("#email").val(''); // reset field after successful submission
$("#msg").val(''); // reset field after successful submission
})
.fail(function(response) {
alert('Error sending message.');
});
return false; // prevent page refresh
});
});
The simple solution for making value global is:
window.modal = "Value";
If you actually don't really need it but just want to store it in a way, where multiple functions will have an access to it you can create it in the scope of the function like:
(function(){
var modal = "value";
$.ajax({
success: function() {
console.log(modal);
}
})
})()

Facebook post as page

I know this question was asked already often, but I have been trying to fix this problem for like more than 6 hours now and I could use a little help. Error Message: Insufficient permssion to post to target on behalf of the viewer.
I'm trying to post as page, my code so far:
var pageID = "myPage";
FB.init({
appId: "myId",
secret: "mySecret",
status: true,
cookie: true,
});
function postToFeed() {
FB.login(function(response) {
FB.api('/me/accounts', function(response) {
var accessToken = "";
var data = response.data;
for (var i=0; i<data.length; i++) {
if (data[i].id == pageID) {
accessToken = data[i].access_token;
}
}
if (accessToken == "") {
alert("You are not allowed to post.");
}
else {
console.log(accessToken);
FB.api('/' + pageID + '/feed', 'post', {
message : "test",
link : 'Link',
picture : 'Imageurl',
name : 'test',
to : pageID,
from : pageID,
description : 'test'
}, function(response) {
if (!response || response.error) {
alert(JSON.stringify(response.error));
} else {
alert('Post ID: ' + response.id);
}
});
}
});
}, {scope: 'publish_actions,manage_pages,public_profile'});
}
When I echo my accessToken via console and copy the accessToken inside this code, it works perfectly. But that's not an solution because I want it to be dynamic:
function postToFeed() {
FB.login(function(response) {
FB.api('/' + pageID + '/feed', 'post', {
access_token: 'theAcccessToken',
message: "I'm a Page!",
}, function(response) {
console.log(response);
}
);
}, {scope: 'publish_actions,manage_pages,public_profile'});
}
And this does not work? (not sure what you want to do with the "to" and "from" parameters, so i removed them)
FB.api('/' + pageID + '/feed', 'post', {
message : 'test',
link : 'Link',
picture : 'Imageurl',
name : 'test',
description : 'test',
access_token: accessToken
}
The most important thing: You need publish_pages to post "as Page", not publish_actions.
Btw, a small optimization:
for (var i = 0; i < data.length; i++) {
if (data[i].id === pageID) {
accessToken = data[i].access_token;
break;
}
}
if (accessToken === "") {
alert("You are not allowed to post.");
}
...always use typesafe comparison :)

Can't send an email with Parse Cloud Code and Mandrill

New to coding and never used Cloud Code before.
I need to send a confirmation email when someone submits their email in a form on my webpage with Parse Cloud Code but I can't get it to work. I'm using the Mandrill Cloud Module to send the emails.
My questions are -
a) Am I calling the Cloud Function correctly?
b) The only variable that changes is the persons email address. Am I passing that variable correctly?
Example code would really help.
Thanks
Here's my Cloud Code:
Parse.Cloud.define("introEmail", function(request, response) {
var Mandrill = require('mandrill');
Mandrill.initialize('*************');
mandrill.sendEmail({
message: {
text: "Hello!",
subject: "Thanks for Signing Up!",
from_email: "Test#Test.com",
from_name: "Chad",
to: [
{
email: request.params.Address,
name: ""
}
]
},
async: true
}, {
success: function(httpResponse) { response.success("Email sent!"); },
error: function(httpResponse) { response.error("Uh oh, something went wrong"); }
});
});
Here's my JS code:
$(".input-group-btn").click(function() {
console.log("Notify Me");
var Address = $(".form-control").val();
var Email = Parse.Object.extend("Email");
var email = new Email();
email.set("Address", Address);
console.log(Address);
email.save(null, {
success: function(email) {
console.log('New object created with objectId: ' + email.id);
Parse.Cloud.run(introEmail,Address)
},
error: function(email, error) {
alert('Could not accept email address: ' + error.message);
}
});
});
a) Call the Cloud function with Parse.Cloud.run(), however, you must pass name, data, options. These are 'introEmail' (the name of the cloud function), the variable which is {Address: $(".form-control").val()} and the success/error handlers.
b)See a
Parse.Cloud.define("introEmail", function(request, response) {
var mandrill = require("mandrill");
mandrill.initialize('*************');
mandrill.sendEmail({
message: {
text: "Hello!",
subject: "Thanks for Signing Up!",
from_email: "Test#Test.com",
from_name: "Test",
to: [
{
email: request.params.Address,
name: ""
}
]
},
async: true
}, {
success: function(httpResponse) { response.success("Email sent!"); },
error: function(httpResponse) { response.error("Uh oh, something went wrong"); }
});
});
Here's the JS for the client-side:
$(".input-group-btn").click(function() {
console.log("Notify Me");
var Address = $(".form-control").val();
var Email = Parse.Object.extend("Email");
var email = new Email();
email.set("Address", Address);
console.log(Address);
email.save(null, {
success: function(email) {
// Execute any logic that should take place after the object is saved.
console.log('New object created with objectId: ' + email.id);
// Invoke our cloud function, using the phone number in the text field
Parse.Cloud.run('introEmail', {
Address: $(".form-control").val()
}, {
// Success handler
success: function(message) {
alert('Success: ' + message);
},
// Error handler
error: function(message) {
alert('Error: ' + message);
}
});
}
});
});

Facebook story posting issue

I am setting up for posting story on facebook but I am getting following alert:
You, or this app's Open Graph Test User, must have published this action at least once.
I am getting id in the response but when I am clicking on that I am redirecting to "Page not found".
Here is my code :
FB.api(
'me/objects/appnamespace:objecttype',
'post',
{
object :{
app_id: myappId,
objectype: myurl,
title: "Sample Organization",
image: mylogo,
description: ""
}
},
function(response) {
if (!response) {
alert('Error occurred.');
} else if (response.error) {
document.getElementById('result').innerHTML =
'Error: ' + response.error.message;
} else {
document.getElementById('result').innerHTML =
'<a href=\"https://www.facebook.com/me/activity/' +
response.id + '\">' +
'Story created. ID is ' +
response.id + '</a>';
}
}
);

Categories

Resources