getting twitter username with access token - php

I've a php application and successfully create a function to save access tokens and secrets in the database. (It's about sending specific content to their profile)
I want to show them which twitter account they linked to my application. How can I get their twitter user with access token?
Thanks for help.

When you get the access token Twitter also returns a screen_name and user_id variables. You can use those instead of making an additional request to GET account/verify_credentials

Use this instead:
$credentials = $connection->get('account/verify_credentials')
and then to get username, name, and location-
$name = $credentials->name;
$username = $credentials->screen_name;
$location = $credentials->location;

Related

Get the Email-Id of the Client PHP

I want So I've followed the instructions given on the following page
https://developers.google.com/api-client-library/php/auth/web-app
However, it isn't too clear on how to get the currently signed in user's email id. The current scopes that I've set are for reading the person's profile, his email-id and gmail.readonly (reading all emails).
My question is, say I have the access token, and I've initialized the Google_Client object by setting the access token, how do I get the currently sign-in user's email?
Heh, looks like I just needed to find the proper Google Service. Got this by going through the documentation.
https://developers.google.com/resources/api-libraries/documentation/gmail/v1/php/latest/class-Google_Service_Gmail.html
The code now is:
$gmail = new Google_Service_Gmail($client);
if($client->getAccessCode()) {
$token_data = $client->verifyAccessToken();
$email = $token_data['email'];
}
Also, for some reason, the line with $token_data doesn't seem to work, so I ended up using the REST API to verify the access token. Just replace that line with
$token_data = json_decode(file_get_contents('https://www.googleapis.com/oauth2/v1/tokeninfo?access_token='.urlencode($client->getAccessToken()["access_token"])), true);

LinkedIn Access Token to authenticate users and use JSAPI Send a message

I am working on the PHP application, that captures "Access Token", when user click on register with Linkedin, I use that access token to get all the related information from the user's Linkedin profile.
I now want to include LinkedIn Send a message feature http://developer.linkedin.com/documents/sample-code-sending-message.
Is there any way I can use the Send a message feature with authentication process using user's access token without showing user to login with linkedIn again to send a message?
Yes there is, just store the token in a database (it should be good for 60days) and reuse it in your requests. If the token expires you will have to reauthorize a new one.
Example with my linkedin class from:
https://github.com/EJTH/SLinkedIn
But it should be pretty similar with other classes or extensions.
$ln = new SimpleLinkedIn('APIKEY','APISECRET');
$ln->addScope('rw_nus');
$ln->setTokenData($myUserObj->getLinkedinToken() /* Get token from db */);
if($ln->authorize()){
/* Do OAuth stuff */
$user = $ln->fetch('GET', '/v1/people/~:(firstName,lastName)');
$tokenData = $ln->getTokenData();
/* Save the new token, if it was changed */
$myUserObj->setLinkedinToken($tokenData['access_token']);
} else {
/* User declined authorization */
}
Just remember that your token must have the scope for the action you want to perform.
Send message
how do i send a message/notification with the linkedin api?
You use LinkedIn Access Token and send by function message.
<?php
// base or site url is the site where code linked in button is kept for signup
$baseURL = 'http://localhost/linkedin/';
/* callback or redirect url is the page you want to open
* after successful getting of data i.e. index.php page
* (must be same in linkedin dashboard) */
$callbackURL = 'http://localhost/linkedin/process.php';
// APP ID (will receive from linkedin dashboard)
$linkedinApiKey = 'your api key';
// APP Client
$linkedinApiSecret = 'your api secret';
// This is fixed no need to change
$linkedinScope = 'r_basicprofile r_emailaddress';
?>

Getting 403 error when trying to update profile description with Soundcloud PHP API

When I try to update the profile description of a soundcloud account via their php sdk, I get a 403 error every time. The app is authenticated and I am able to do things like place comments, but I'm not able to update anything on the profile (particularly the description field).
I'm using the standard code, found in their official documentation:
<?php
require_once 'Services/Soundcloud.php';
// create a client object with access token
$client = new Services_Soundcloud('YOUR_CLIENT_ID', 'YOUR_CLIENT_SECRET');
$client->setAccessToken('YOUR_ACCESS_TOKEN');
// get the current user
$user = json_decode($client->get('me'));
// update the user's profile description
$user = json_decode($client->post('me', array(
'description' => 'I am using the SoundCloud API!'
)));
print $user->description;
Please help me find out where the error comes from, because I'm all out of ideas.
Our bad, the user documentation that you point to there had two problems:
Updates to the user resource should use the PUT method, not POST.
Arguments need to be namespaced properly.
I've modified the documentation to fix these two problems. New code sample:
<?php
require_once 'Services/Soundcloud.php';
// create a client object with access token
$client = new Services_Soundcloud('YOUR_CLIENT_ID', 'YOUR_CLIENT_SECRET');
$client->setAccessToken('ACCESS_TOKEN');
// get the current user
$user = json_decode($client->get('me'));
// update the user's profile description
$user = json_decode($client->put('me', array(
'user[description]' => 'I am using the SoundCloud API!'
)));
print $user->description;
Hope that helps and sorry again for the confusion. Let me know if you run into any more problems.
To update user related information you need to login to Sound Cloud as user and then authenticate your application to use your personal data, else you will get 403 for all user related information while updating / delete. For posting any comments you don't need this authentication
http://developers.soundcloud.com/docs#authentication
Refer
Getting Information about the Authenticated User
Once the user has signed into SoundCloud and approved your app's authorization request, you will be able to access their profile and act on their behalf. We have provided a convenient endpoint for accessing information about the authenticated user.

access graph data without forcing user to login

I am looking to pull data from a fb page to a website without having a user authenticate.
The data I wish to display on a webpage is the event listing for a fb page, both the page and the events are open, and I can grab the page and event data ok, eg
http://graph.facebook.com/PAGEID
http://graph.facebook.com/EVENTID
But to get a list of the events in order to get the eventIDs an access token is required eg
http://graph.facebook.com/PAGEID/events
Is this possible to get this data without having a user login to fb and authorise with an application ?
I have looked at just saving an access token but this seems to expire.
any suggestions/pointers would be great, thanks in advance
Providing a quick solution to this set up an app and get an access token, note it would be best to cache the result data so as to not make an api call on each page load
<?php
//set your app id, client_secret and fb id
$client_id = 'xxx';
$client_secret = 'xxx';
$fbID = 'xxx';
//get an access token
$access_token = file_get_contents("https://graph.facebook.com/oauth/access_token? type=client_cred&client_id=".$client_id."&client_secret=".$client_secret);
//use this access token to return the events
$jsonurl = "https://graph.facebook.com/$fbID/events?$access_token";
$json = file_get_contents($jsonurl,0,null,null);
// do stuff with $json
?>

Get user details from openid

I'm using lightopenid as the login system for a site and after successful login, I need the user's details like his first name, last name, email and date of birth..
How can I get this information from his openid? Suppose for google, I'm using the authentication url as: https://www.google.com/accounts/o8/id
Then after the validate() method returns 1, I'm redirecting the user to another page in my site. But how can I fetch the details of the user after login ?
FYI, I'm using openid for google, yahoo and aol.
And for facebook, I'm using graph api and for twitter, I'm using twitter oauth. Is there any way of fetching user data with these too? Please suggest.
Just read the manual:
http://code.google.com/p/lightopenid/wiki/GettingMoreInformation
$openid->required = array('namePerson/friendly', 'contact/email');
$openid->optional = array('namePerson/first');
before calling $openid->authUrl()!
Then
$openid->validate();
$userinfo = $openid->getAttributes();
$email = $userinfo['contact/email'];
$firstName = $userinfo['namePerson/first'];
You need to add a parameter to specify that you also want to receive data back from the OpenID request.
I append the following to my OpenID requests to get the email details.
&openid.ns.ax=http%3A%2F%2Fopenid.net%2Fsrv%2Fax%2F1.0&openid.ax.mode=fetch_request&openid.ax.type.email=http://axschema.org/contact/email&openid.ax.required=email
The first part specifies the namespace being used for the extended data.
The second part specifies that we are making a fetch request for the data.
The third part specifies the schema we are using for the email.
And the final part is specifying that we require the email to be returned.
I have tested this with Google and it works fine. I do not have the other accounts, so have not tested it for those.
OAuth and Facebook Graph API will have there own formats, so I am not sure on those ones.
$openid->identity = 'https://www.google.com/accounts/o8/';
// use the following line to obtain the required details. These are the only details that google mail provides.
$openid->required = array('namePerson/friendly', 'contact/email' , 'contact/country/home', 'namePerson/first', 'pref/language', 'namePerson/last');
header('Location: ' . $openid->authUrl());
Seemingly lightopenid provides a method for that:
$openid->validate();
$userinfo = $openid->getAttributes(); // associative array
It returns either SimpleReg or "Attribute Exchange" data. But only if the user agreed to that, I would hope.

Categories