Using username in facebook graph api post - android

I am posting to user's wall using following code using restFB.
String myAccessToken = null;
String appId="my app id ......";
String appSecret="my app secret";
// facebook user id of the user on whose wall to post
String userId="user id ...";
DefaultFacebookClient dfc = new DefaultFacebookClient();
AccessToken accessToken = dfc.obtainAppAccessToken(appId,appSecret);
myAccessToken = accessToken.getAccessToken();
FacebookClient facebookClient = new DefaultFacebookClient(myAccessToken);
FacebookType publishMessageResponse = facebookClient.publish(userId+"/feed", FacebookType.class, Parameter.with("message", "test"));
System.out.println("Published message ID: " + publishMessageResponse.getId());
All is working fine.
But now I want to include user's facebook first name in the post. Is there any way to do so? I dont want to generate yet another request to facebook for getting the username, as I am already generating 2 requests for getting user id(from redirect url). Is there any mechanism that graph api itself replaces some kind of tag in the post text with user name.
E.g I send the following text to post on user wall:
#username# is doing something...
And it is replaced by facebook graph api
Mark is doing something....

I dont want to generate yet another request to facebook for getting the username, as I am already generating 2 requests for getting user id(from redirect url).
No you can't. You have to make the request to fetch the name. /me?fields=name
But why does it matter, it'll take very less time to query this.

Related

Redirect url for Google OAuth Android

Never really worked with OAuth, trying to implement it now, I want to get access token and profile data from google and facebook. Using Xamarin.Auth.
With Facebook there're no problems, I specify "http://www.facebook.com/connect/login_success.html" as redirect url and after I login it goes back to the activity I was before.
However with Google it's not as smooth - couldn't find any similar to facebook login success pages, somewhere found suggestion to use "https://www.googleapis.com/plus/v1/people/me" - added it to redirect url white list, however after sign in I would get "Redirect_url_mismatch" A native application: application nameaccording to their documentation I should use "my.package.name:" and again I added that to redirect url white list, attempted to sign in, this time after sign in screen I get to second screen where I need to confirm read permissions and after that I get very short error something like "com.my.package:/?oauthparameterX=value1...." and get redirected to permission screen again.
Here's my complete OAuth2Authenticator:
var auth = new OAuth2Authenticator(
clientId: SocialIds.GooglePlusId,
clientSecret: SocialIds.GooglePlusSecret,
scope: OAuthUrl.GoogleScope,
authorizeUrl: new Uri(OAuthUrl.GoogleAuthorize),
redirectUrl: new Uri("https://www.googleapis.com/plus/v1/people/me"),
accessTokenUrl: new Uri("https://accounts.google.com/o/oauth2/token"),
getUsernameAsync: null);
auth.AllowCancel = false;
urls:
public static string GoogleAuthorize = "https://accounts.google.com/o/oauth2/auth";
public static string GoogleScope = "https://www.googleapis.com/auth/userinfo.email";
public static string GoogleRedirect = "https://www.googleapis.com/plus/v1/people/me";
public static string GoogleUserInfo = "https://www.googleapis.com/oauth2/v1/userinfo?access_token={0}";
In the above listed code, you are not giving the redirect uri instead you are giving the scope of google api. The purpose of redirect uri is to recieve the response from google api after authorization. The response should be a code. This response code is used for accessing the access_token,refresh_token, id_token etc. So you have to recieve this code in your project side.For this purpose , the redirect uri is used.
Go to your google console, create project, add credentials, then you will be redirect to a page conatains,
You can find the authorized redirect url. Give the url , then configure your code with new redirect url . Everything will be fine after this.

How to display logged in user's profile picture in android app using ProfilePictureView from Facebook SDK?

I want to display the profile picture of a logged in user in my android app.How to do that using the method ProfilePictureView in Facebook SDK?Please help...
If you want to display the user's photo using the CircularImageView library. You can still make requests to the Facebook Graph API and parse the response by yourself. You can do this manually or using the Android SDK for Facebook by making a request to the following endpoint...
graph.facebook.com/v2.1/me/picture
of course, you can replace me by a specific user's id. You don't need to request any special permissions in the access token. The request about will render a json response similar to the one below...
{
"data": {
"url": "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash2/v/t1.0-1/blahblahblah_n.jpg?oh=2xgdgfdhgdhdhg33ba58e021&oe=5dghgdh_gda__=1dgfhdfh88_b9gdhfghfghfghfghfgh408d6e7",
"is_silhouette": false
}
}
this json response contains the following fields:
url: which is a url from where you can request the profile picture
is_silhouette: which determines whether the picture is the default facebook silhouette or not. If the user has not added a profile picture, then the facebook default silhouette will be returned in the url...see below
You can also use the Android SDK for Facebook, create a request object, pass in the current session object, execute the request asynchronously, for example...
new Request(
_currentSession,
"/me/picture",
null,
HttpMethod.GET,
new Request.Callback() {
public void onCompleted(Response response) {
//parse the response
}
}
).executeAsync();
For more information you can read the documentation on their site and even use the Graph API Explorer for testing purposes

Not able to Get twitter Profile pic of users api 1.1

I want to get Profile picture from twitter api.
i have tried the following methods .
a) as per this url :Twitter4j authentication credentials are missing and Retrieve the user profile image from twitter
//Twitter Conf.
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey(CONSUMER_KEY)
.setOAuthConsumerSecret(CONSUMER_SECRET)
.setOAuthAccessToken(ACCESS_KEY)
.setOAuthAccessTokenSecret(ACCESS_SECRET);
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
User user = twitter.showUser(id of other user who using same app);
user.getOriginalProfileImageURL()
in this method get Exception
08-29 09:05:26.282: W/System.err(3823): 404:The URI requested is invalid or the resource requested, such as a user, does not exists. Also returned when the requested format is not supported by the requested method.
08-29 09:05:26.282: W/System.err(3823): message - Sorry, that page does not exist.
b) as per this latest url
https://dev.twitter.com/docs/api/1.1/get/users/profile_banner
pass screen name of user here :
https://api.twitter.com/1.1/users/profile_banner.json?screen_name=iRohitSachdeva
response: {"errors":[{"message":"Bad Authentication data","code":215}]}.
The correct thing to do is work out why your authentication data is going wrong.
However, the simple way to get profile images is using a service like http://avatars.io/
For example, the get my avatar, visit http://avatars.io/twitter/edent
It should return the correct URL for my Twitter account's pic.

Google authentication using Xamarin.Auth

I'm developing an application using Xamarin.Android.
I need to enable login with Facebook and Google and I choose to use the Xamarin.Auth component.
I got it work with facebook but I have some issue with Google.
This is my code:
var auth = new OAuth2Authenticator (
clientId: "myId.apps.googleusercontent.com",
clientSecret: "mysecred",
scope: "openid",
authorizeUrl: new Uri ("https://accounts.google.com/o/oauth2/auth"),
redirectUrl: new Uri ("myredirect:oob"),
accessTokenUrl: new Uri ("https://accounts.google.com/o/oauth2/token"),
getUsernameAsync: null
);
activity.StartActivity (auth.GetUI (activity));
auth.Completed += (sender, e) => {
Console.WriteLine (e.IsAuthenticated);
};
Is this way the Google Activity is displayed, and I can insert my Username and password. But when I click the google login button I get a message like this:
google auth please copy this code switch to your application and paste it there [code]
What I have to do with that code? I just need to get the user name/lastname/email/id.
Thanks a lot!
Bellow steps worked for me.
1.register in google developer console as webapplication instead of installed application(android)* provide the redirect url with valid url ("http://abcd.com/xyz.aspx") same should be used in the application code.
2.on authentication complete it will return access_token
3.by using the access_token make the REST request to get user complete information (https://www.googleapis.com/oauth2/v1/userinfo?access_token=" + accessTokenValue + “.)
4.Deserialize the json response to get information in object.
check the source code :http://appliedcodelog.blogspot.in/2015/08/login-by-google-account-integration-for.html

construct predefined messages with properties to post on Facebook android

I want to post a pre-defined message on Facebook through my android application. I got everything to work except the 'properties' field. I want to post a message where it says:
More information: here
and when the user clicks on 'here', it should link to the page.
This is what I did:
Bundle params = new Bundle();
String s2 = "{'More information':{'text':'here', 'href':" + details + "}}";
params.putString("properties", s2);
where 'details' is the link to the page.
But it seems like facebook is not picking up this line. I successfully set up the caption, picture and other fields.
Any insights? Thanks!
This is by design, as far as I know, we do not support HTML in status updates. We will automatically create a link if you post a valid URL however, so I would suggest just pasting out the full link
".....More information: (www.yourURLhere.com)"
On Facebook, www.yourURLhere.com will be a clickable link.

Categories

Resources