Facebook app friends list Laravel + Facebook SDK - facebook-php-sdk

I am using Laravel + Facebook SDK ( https://github.com/SammyK/LaravelFacebookSdk) to get a friends list from my app. The weird thing is for some reason that I don't know why, some users I can fetch their friends, some users I can't.
For example, under my own user, I can fetch all my friends who are also using my app.
However, I have created a new user in Facebook just for testing. I've became a friend of this new user. In addition, this user has approved the app in their facebook. As a result, I can't see this user in my friends list and also this new user can't see me in their friends list.
My code base is
$q = \Facebook::get('/me/friends', $token);
$friends = json_decode($q->getBody())->data;

Facebook V2.0 upgraded. So you can try like this "user_friends".
Refer Link

I just figure out what was wrong! I was logging in the Facebook without the right permission scope.
Every login it must pass as a parameter "user_friends" according to this url https://developers.facebook.com/docs/facebook-login/permissions and https://developers.facebook.com/docs/php/howto/example_facebook_login
Basically, the code is
$fb = new Facebook\Facebook([
'app_id' => '{app-id}', // Replace {app-id} with your app id
'app_secret' => '{app-secret}',
'default_graph_version' => 'v2.2',
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email']; // Optional permissions
$loginUrl = $helper->getLoginUrl('https://example.com/fb-callback.php', $permissions);

Related

Symfony2 PHPunit testing functions which gets user object from token storage

I know this question is already asked but I can't get it to work and I can't see what I'm doing wrong.
I'm trying to write a test for a function which depends on a user logged in and gets user object from security.token_storage but unfortunately I can't get it work. For setting up the token my code is
$token = new UsernamePasswordToken(
$person,
$person->getPassword(),
'sso',
$person->getRoles()
);
$containerInterface->get('security.token_storage')->setToken($token);
$containerInterface->get('event_dispatcher')->dispatch(
AuthenticationEvents::AUTHENTICATION_SUCCESS,
new AuthenticationEvent($token)
);
where $person is user object and 'sso' is firewall name. When I run a test where I get user object from token_storage I get null.
If I understood correctly, you're missing the login event
$event = new InteractiveLoginEvent($request, $token);
$this->get("event_dispatcher")->dispatch("security.interactive_login", $event);
Also, but I'm not sure of, you don't need this snippet
$containerInterface->get('event_dispatcher')->dispatch(
AuthenticationEvents::AUTHENTICATION_SUCCESS,
new AuthenticationEvent($token)
);

WooCommerce REST Client API - Programmatically get consumer key and secret

I am currently using the client-API to implement a simple user front-end to upload products. The function client->products->create() seems to work fine, how ever I can’t get around one issue.
Every time I upload a product, the vendor is set to the admin user instead of the user that is currently logged in. Is there a way to set the vendor through the API? Has anybody get done this?
This is the function I created that is called by AJaX when the form is submitted (I left key and website fields empty here on purpose):
function addProduct()
{
$options = array(
'debug' => false,
'return_as_array' => false,
'validate_url' => false,
'timeout' => 30,
'ssl_verify' => false,
);
try {
$client = new WC_API_Client('', '', '', $options);
$productName = $_POST["productname"];
$price = $_POST["price"];
$discountPrice = $_POST["discountPrice"];
$description = $_POST["description"];
$shortDescription = $_POST["shortDescription"];
$authorId = 5;
$client->products->create(array('title' => $productName, 'type' => 'simple', 'regular_price' => $price, 'description' => $description));
} catch (WC_API_Client_Exception $e) {
echo $e->getMessage() . PHP_EOL;
echo $e->getCode() . PHP_EOL;
if ($e instanceof WC_API_Client_HTTP_Exception) {
print_r($e->get_request());
print_r($e->get_response());
}
}
echo ("Publicado" . $authorId);
// Una función AJaX en WordPress debe siempre terminarse con die().
die();
}
The problem seems to be the consumer key and consumer secret, so, is there a way to programmatically provide the clients with API keys and get these dynamically?
UPDATE: The method to obtain the consumer key described below will not work; it is no longer possible to get hold of the consumer key from the database once it has been generated. The consumer key stored in this new table is not the same consumer key that is generated in the admin screens and passed out to the end user. It appears to be an SHA256 hashed a version of this key. This is more secure (previously the consumer key and secret stored in wp_usermeta was tantamount to storing clear-text passwords, as anyone with access to that data would be able to log into the API as any of those users), but is a little less convenient. Win some, lose some, but win on security.
Your new WC_API_Client() will take three parameters before the options: $store_url, $consumer_key and $consumer_secret.
Any user on the WC shop who is to be used to access the API will need a consumer key or consumer secret. The consumer key will identify which user the API will run as, and it is that user which will be linked to any entities created through the API.
Until recently, you could get these two pieces of information for a user like this:
$consumer_key = get_user_meta($user_id, 'woocommerce_api_consumer_key', true);
$consumer_secret = get_user_meta($user_id, 'woocommerce_api_consumer_secret', true);
Where $user_id is the ID for the user that will be creating items. If you want the current logged in user to be able to create items in their name then that user would need to be given a consumer key and secret, and would need to be in an appropriate WC/WP group to give them permission to do so.
Note, that if you do this, then the user will also have access to the admin pages for WC to create these items, and not just through the API.
In later versions of WC, the user meta items have been moved to a separate table: wp_woocommerce_api_keys so you need to look in there instead of in the user meta.
This will get you the consumer key and secret for a given user ID:
global $wpdb;
$key = $wpdb->get_row( $wpdb->prepare("
SELECT consumer_key, consumer_secret, permissions
FROM {$wpdb->prefix}woocommerce_api_keys
WHERE user_id = %d
", $user_id), ARRAY_A);
the results being something like this:
array(3) {
["consumer_key"]=>
string(64) "58043812eee6aa75c80407f8eb9cec025825f138eb7d60118af66cf4b38060fa"
["consumer_secret"]=>
string(43) "cs_1da716412bb9680d8b06b09160872b7e54416799"
["permissions"]=>
string(10) "read_write"
}
I am, of course, assuming you are using the API to "loop back" to the current site and not accessing a remote site. Using the WC API to create products even on the current site can be very much more convenient than going through the PHP object API.
I have not yet found any public WC methods to get these details; they are all private and assume only WC needs to know these details.
Yes there is a fine customization that you need to do in your code that is as follows:
Background information:
Each users Consumer Key,Consumer Secret Key and read/write permissions (if WooCommerce API Keys are generated for that users) are stored in wordpress's usermeta table with a meta_keys as 'woocommerce_api_consumer_key', 'woocommerce_api_consumer_secret' and 'woocommerce_api_key_permissions' respectively.
So you just need to get the current users id first then get that user's meta value as mention above assign to some variables and send them as a parameter.
I think the problem is generate programmatically the API keys for that customer for witch you want consume the woocommerce service, because the keys ar owned for each users and there aren't be useful for other users.
My advice is looking admin source code of woocommerce.

how customize drupal 7 for multi user instance

When generating a new drupal site (multisite), I would like to define the default user profile as an admin user without acces to administration menu.
How can I create this new user as an install profile with default login infos, keeping at the same time the admin user?
i found this node ,but i can't understand ! how can I apply !?
Thank you for suggestions.
This tutorial lays out how to create roles and accounts in an install profile.
The core idea is to programmatically create a new role and user account in the profile .install file. For example (untested):
// Create a role for site content managers
$admin_role = new stdClass();
$admin_role->name = 'content manager';
$admin_role->weight = 2;
user_role_save($admin_role);
// Grant all admin permissions
user_role_grant_permissions($admin_role->rid, array_keys(module_invoke_all('permission')));
// Revoke the toolbar permissions
user_role_revoke_permissions($admin_role->rid, array(
'access administration menu', // The admin_menu module toolbar
'access toolbar', // The core toolbar
));
// Create a user with the content manager role
$new_user = array();
$new_user['name'] = 'manager';
$new_user['mail'] = 'manager#example.com';
$new_user['roles'] = array($admin_role->rid => $admin_role->rid);
$new_user['pass'] = 'incredibly-secure-password-here';
$new_user['status'] = 1;
$account = user_save(drupal_anonymous_user(), $new_user);

programmatically login symfony2 after registration

i try to login the user manually after the registration with this code:
http://www.michelsalib.com/2011/04/pragmatically-authenticate-the-user-in-symfony2/
Its not working with the fresh created user, but when i login an other existing user after a userregistation, everything works fine.
I thought the user object isn't complete but i tried to read the registrerd user again and it doesn't work too.
Has anyone an idea what the problem is?
Thank you very much
You have to create a new token and pass it to the security context.
// Create a new token
$token = new UsernamePasswordToken($user, $credentials, $providerKey, $user->getRoles());
// Retrieve the security context and set the token
$context = $this->container->get('security.context');
$context->setToken($token);

Drupal 'Send Email' advanced action

I may be missing something blindingly obvious here (I hope so).....I am creating a module in Drupal 6 that consists of some triggers and actions. in it's simplest form it consists of:
An action which checks for some criteria (event that needs to be triggered once a month per user)
A trigger which is fired for each user that the criteria is true for
I would like as much as possible to be managed through the triggers / actions interface in Drupal as the site admin is not a developer. The plan is to use the cron trigger to fire the action in 1. which will then fire a trigger for each user. The site admin will then be able to create a Send Email action through the actions interface and hook it up to the trigger from 2.
The part I can't get my head around is how the recipient of the email will be specified - the user trigger will be fired from an action run by cron (i.e. not in any user context) - how can I pass in a variable that can be used here?
Thanks,
Triggers fire actions not the other way around.
The user that you pass to actions_do dosn't have to be the logged in user. You can query for the users that you want to email and loop thrhough them doing user_load and then an actions_do
something like
foreach ($user_ids as $uid) {
$context_user = user_load(array('uid' => $uid));
$context = array(
'hook' => 'myhook',
'op' => $op,
'user' => $context_user,
);
actions_do(array_keys($aids), $context_user, $context);
}

Resources