I am trying to integrate Facebook ** into my android app. I want to implement **like and share ** facility on **facebook. But i am getting error.
please help me to solve this.
Also I want to know the process of creating New App on Facebook.
I am using code from GitHub. My code is -
public class MyGreatActivity extends Activity {
Facebook facebook = new Facebook("333778590046892");
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
facebook.authorize(this, new String[] { "offline_access", "publish_stream", "raj21kadam#gmail.com" },
new DialogListener() {
#Override
public void onComplete(Bundle values) {
String token=facebook.getAccessToken(); //get access token
// Toast.makeText(this, "token", Toast.LENGTH_LONG).
save(token);
}
#Override
public void onFacebookError(FacebookError error) {}
#Override
public void onError(DialogError e) {}
#Override
public void onCancel() {}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
private void save(String token){
Toast.makeText(this, "token"+token, Toast.LENGTH_LONG).show();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.edit().putString("Token", token).commit();
}
}
I am getting the following error while running the above code -
Dialog Errorcom.facebook.android.DialogError: The connection to the server was unsuccessful.
try this Single Sign On (SSO) Using Android Native Client For Facebook .
paste this
facebook.authorize(this, new DialogListener() {
#Override
public void onComplete(Bundle values) {
String token=facebook.getAccessToken(); //get access token
// Toast.makeText(this, "token", Toast.LENGTH_LONG).
save(token);
}
public void onFacebookError(FacebookError error) {}
#Override
public void onError(DialogError e) {}
#Override
public void onCancel() {}
});
}
instead of this
facebook.authorize(this, new String[] { "offline_access", "publish_stream", "raj21kadam#gmail.com" },
new DialogListener() {
#Override
public void onComplete(Bundle values) {
String token=facebook.getAccessToken(); //get access token
}
#Override
public void onFacebookError(FacebookError error) {}
#Override
public void onError(DialogError e) {}
#Override
public void onCancel() {}
});
}
Related
Hi Everyone I am intergrating facebook login to my app. I have searched a lot and I am able to login I have used this link: https://developers.facebook.com/docs/mobile/android/build/#register
All I want to do is that When I click on a Button then facebook dialog must appear intead of just opening the activity (HomeActivity) following is my code.
public class HomeActivity extends Activity {
Facebook facebook = new Facebook("114987225319269");
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Button more = (Button) findViewById(R.id.button1);
more.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
facebook.authorize(this, new DialogListener() {
#Override
public void onComplete(Bundle values) {}
#Override
public void onFacebookError(FacebookError error) {}
#Override
public void onError(DialogError e) {}
#Override
public void onCancel() {}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_home, menu);
return true;
}
}
Use this code
Button more = (Button) findViewById(R.id.button1);
more.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (! facebook.isSessionValid()) {
facebook.authorize(HomeActivity.this, PERMISSIONS, new LoginDialogListener());
});
}
this is the login dialog listener
private class LoginDialogListener implements DialogListener {
public void onComplete(Bundle values) {
saveCredentials(facebook);
getAlbumsData task = new getAlbumsData();
task.execute();
}
public void onFacebookError(FacebookError error) {
showToast("Authentication with Facebook failed!");
}
public void onError(DialogError error) {
showToast("Authentication with Facebook failed!");
}
public void onCancel() {
showToast("Authentication with Facebook cancelled!");
}
}
In your Manifest
<activity
android:theme="#android:style/Theme.Translucent"
android:name=".HomeActivity "
>
This will change your activity theme into Dialog theme. Hope this helps...
This is all what you need:
public class BaseActivity extends Activity
{
public AsyncFacebookRunner mAsyncRunner;
public Facebook facebook;
public SharedPreferences mPrefs;
public static String appId = "123344";
SharedPreferences.Editor editor;
private void initialize()
{
facebook = new Facebook(appId);
mAsyncRunner = new AsyncFacebookRunner(facebook);
}
public void verifyLogin()
{
mPrefs = getPreferences(MODE_PRIVATE);
String accessToken = mPrefs.getString("access_token", null);
facebook.setAccessToken(accessToken);
long expires = mPrefs.getLong("access_expires", 0);
if (accessToken != null)
{
facebook.setAccessToken(accessToken);
}
if (expires != 0)
{
facebook.setAccessExpires(expires);
}
if (!facebook.isSessionValid())
{
facebook.authorize(this, new String[] {
"email", "publish_stream", "create_event"
}, new DialogListener()
{
#Override
public void onCancel()
{
// Function to handle cancel event
}
#Override
public void onComplete(Bundle values)
{
// Function to handle complete event
// Edit Preferences and update facebook acess_token
editor = mPrefs.edit();
editor.putString("access_token", facebook.getAccessToken());
editor.putLong("access_expires", facebook.getAccessExpires());
editor.commit();
}
#Override
public void onError(DialogError error)
{
// Function to handle error
}
#Override
public void onFacebookError(FacebookError fberror)
{
// Function to handle Facebook errors
}
});
}
}
}
I am using facebook connect in my android app. It is basically a form. There is a facebook login button on the page. I want the information on the form to be filled in when thee user logs in using Facebook login.
I am trying the following code:
public void loginFB() {
facebook.authorize(this, new String[] { "email", "read_stream" }, new DialogListener() {
#Override
public void onComplete(Bundle values) {}
#Override
public void onFacebookError(FacebookError error) {}
#Override
public void onError(DialogError e) {}
#Override
public void onCancel() {}
});
getlogininfo();
}
public void getlogininfo(){
mAsyncRunner.request("me", new RequestListener(){
#Override
public void onComplete(String response, Object state) {
try{
Log.d("Profile", response.toString());
JSONObject json = Util.parseJson(response);
final String fname1 = json.getString("first_name");
final String lname1 = json.getString("last_name");
final String email = json.getString("email");
MintPaymentsActivity.this.runOnUiThread(new Runnable(){
public void run() {
fname.setText(fname1);
lname.setText(lname1);
emailadd.setText(email);
}
});
}
catch(JSONException e){
Log.w("This", "eror");
}
}
#Override
public void onIOException(IOException e, Object state) {
// TODO Auto-generated method stub
}
#Override
public void onFileNotFoundException(FileNotFoundException e, Object state) {
// TODO Auto-generated method stub
}
#Override
public void onMalformedURLException(MalformedURLException e, Object state) {
// TODO Auto-generated method stub
}
#Override
public void onFacebookError(FacebookError e, Object state) {
// TODO Auto-generated method stub
}
});
}
/*FB --- */
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
basically, when the user clicks the button, faceboook asks for login and then collects the JSON data to display the name and email of the logged in user.
It gives me the following error:
05-30 21:52:40.598: E/AndroidRuntime(665): FATAL EXCEPTION: Thread-13
05-30 21:52:40.598: E/AndroidRuntime(665): com.facebook.android.FacebookError: An active access token must be used to query information about the current user.
05-30 21:52:40.598: E/AndroidRuntime(665): at com.facebook.android.Util.parseJson(Util.java:279)
05-30 21:52:40.598: E/AndroidRuntime(665): at com.Myapp.Myapp$2.onComplete(Myapp.java:117)
05-30 21:52:40.598: E/AndroidRuntime(665): at com.facebook.android.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:254)
When I put an extra button to fill in the details, so that the getlogininfo() is implemented on a button click (after logging in) it works fine. But i want it to be filled in as soon as the user clicks the facebook login button.
It should be:
public void loginFB() {
facebook.authorize(this, new String[] { "email", "read_stream" }, new DialogListener() {
#Override
public void onComplete(Bundle values) {
this.getlogininfo();
}
#Override
public void onFacebookError(FacebookError error) {}
#Override
public void onError(DialogError e) {}
#Override
public void onCancel() {}
});
}
You want to execute getLogininfo only after the authentication has completed.
Also, I suggest that you handle the errors as well, at least log it so you'll know when developing.
I am using facebook.authorize(activity,new Dialog(...)) in
public class BirthdayActivity extends Activity {
...
facebook.authorize(this, new DialogListener() {
#Override
public void onComplete(Bundle values) {
}
#Override
public void onFacebookError(FacebookError error) {}
#Override
public void onError(DialogError e) {}
#Override
public void onCancel() {}
});
}
It works perfectly here, but when I do the same in
public class StartBirthdayApplication extends Service {
...
public void onStart(Intent intent, int startId) {
facebook.authorize(this, new DialogListener() {
public void onComplete(Bundle values) {
}
#Override
public void onFacebookError(FacebookError error) {}
#Override
public void onError(DialogError e) {}
#Override
public void onCancel() {}
});
...
}
I get the error
Key message expected byte[] but value was a java.lang.String.The default value <null>was returned.
Attempt to cast generated internal exception:
java.lang.ClassCastException: java.lang.String
What is going wrong?
just pass access token in request.
working correct.
in my activity I use the following code to get Facebook authorization. facebook.authorize is called but it does not enter in one of the listener event (onComplete, onFacebookError...). Anybody could help?
Facebook facebook = new Facebook(FACEBOOK_APPID);
facebook.authorize(ItemDetail.this, new String[] {"publish_stream"},
new DialogListener() {
#Override
public void onComplete(Bundle values) {
Log.i("ItemDetail", "Facebook onComplete");
}
#Override
public void onFacebookError(FacebookError error) {
Log.i("ItemDetail", "Facebook onFacebookError");
}
#Override
public void onError(DialogError e) {
Log.i("ItemDetail", "Facebook onError");
}
#Override
public void onCancel() {
Log.i("ItemDetail", "Facebook onCancel");
}
}
);
You need to put this in your class:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
facebook.authorizeCallback(requestCode, resultCode, data);
}
I'm using the tutorial code...
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import com.facebook.android.*;
import com.facebook.android.Facebook.*;
public class FacebookSSO extends Activity {
Facebook facebook = new Facebook("ID");
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
facebook.authorize(this,new String[] { "offline_access", "publish_stream", "email" },
new DialogListener() {
#Override
public void onComplete(Bundle values) {}
#Override
public void onFacebookError(FacebookError error) {}
#Override
public void onError(DialogError e) {}
#Override
public void onCancel() {}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
}
I'm wondering... once the user has authenticated how would I store the access token? couldn't see any mention of it on the tutorial.
facebook.authorize(this,new String[] { "offline_access", "publish_stream", "email" },
new DialogListener() {
#Override
public void onComplete(Bundle values) {}
String token=facebook.getAccessToken(); //get access token
save(token);
#Override
public void onFacebookError(FacebookError error) {}
#Override
public void onError(DialogError e) {}
#Override
public void onCancel() {}
});
}
private void save(String token){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.edit().putString("Token", token).commit();
}
i.e save it in sharedpreferences
FYI. facebook.authorize had deprecated since Facebook SDK 3
Try in this way
#Override
public void onComplete(Bundle values) {
// Function to handle complete event
// Edit Preferences and update facebook acess_token
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token",
facebook.getAccessToken());
editor.putLong("access_expires",
facebook.getAccessExpires());
editor.commit();
}
#Override
public void onError(DialogError error) {
// Function to handle error
}
#Override
public void onFacebookError(FacebookError fberror) {
// Function to handle Facebook errors
}