Я пытаюсь создать класс, который будет обрабатывать все мои взаимодействия с Facebook.Я передаю созданное мною действие Login в объект FacebookConnector (рассматриваемый объект) для хранения учетных данных и т. Д. Пожалуйста, просмотрите видео, чтобы увидеть, с чем я имею дело * http://www.youtube.com/watch?v=OkHEy9Mh1hc. Ниже приведен класс FacebookConnector сИдентификатор приложения отредактирован
package it.stick;
import java.io.IOException;
import java.net.MalformedURLException;
import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class FacebookConnector {
private static final String APP_ID ="************";
private static final String[] PERMISSIONS = new String[] {"user_likes","read_stream", "user_photos", "email","photo_upload", "offline_access", "publish_actions"};
private static final String TOKEN = "access_token";
private static final String EXPIRES = "expires_in";
private static final String KEY = "facebook-credentials";
private Facebook facebook;
private Activity activity;
/** Saves applications credentials */
public boolean saveCredentials(Facebook facebook){
Editor editor = activity.getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
editor.putString(TOKEN, facebook.getAccessToken());
editor.putLong(EXPIRES, facebook.getAccessExpires());
return editor.commit();
}
public boolean restoreCredentials(Facebook facebook){
SharedPreferences sharedPreferences = activity.getSharedPreferences(KEY, Context.MODE_PRIVATE);
facebook.setAccessToken(sharedPreferences.getString(TOKEN, null));
facebook.setAccessExpires(sharedPreferences.getLong(EXPIRES, 0));
return facebook.isSessionValid();
}
public FacebookConnector(Activity activity){
facebook = new Facebook(APP_ID);
this.activity = activity;
}
// Creates new Facebook session and stores credentials
public void login(){
// 1.Restores previous credentials
//2.Creates and saves new session if previous session is not valid
restoreCredentials(facebook);
if(!facebook.isSessionValid()){
facebook.authorize(activity, PERMISSIONS, new LoginDialogListener());
}
}
public boolean isSessionValid() {
return facebook.isSessionValid();
}
public void logout(){
try {
facebook.logout(activity.getApplicationContext());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void postToWall(String message){
Bundle parameters = new Bundle();
parameters.putString("message", message);
parameters.putString("description", "topic share");
try{
facebook.request("me");
String response = facebook.request("me/feed", parameters, "POST");
Log.d("Tests", "got response: " + response);
if(response == null || response.equals("") || response.equals("false")){
showToast("Blank response from facebook.");
}
else{
showToast("Message posted to your facebook wall.");
}
}
catch(Exception e){
showToast("Failed to post to wall. We fucked up :(");
e.printStackTrace();
}
}
class LoginDialogListener implements DialogListener{
@Override
public void onComplete(Bundle values) {
saveCredentials(facebook);
postToWall("logged on to Stick.it");
}
@Override
public void onFacebookError(FacebookError e) {
showToast("Authentification with Facebook failed!");
}
@Override
public void onError(DialogError e) {
showToast("Authentification with Facebook failed!");
}
@Override
public void onCancel() {
showToast("Authentification with Facebook failed!");
}
}
public void showToast(String message){
Toast.makeText(activity.getApplicationContext(), message, Toast.LENGTH_SHORT).show();
}
}
Вот журнал cat:
02-01 22:39:39.459: W/KeyCharacterMap(637): No keyboard for id 0
02-01 22:39:39.470: W/KeyCharacterMap(637): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
02-01 22:39:46.299: D/dalvikvm(637): GC_EXPLICIT freed 91K, 50% free 2744K/5447K, external 3745K/3903K, paused 1017ms
02-01 22:39:46.999: D/webviewglue(637): nativeDestroy view: 0x303080
02-01 22:39:48.239: D/dalvikvm(637): GC_EXTERNAL_ALLOC freed 54K, 51% free 2706K/5447K, external 3331K/3903K, paused 735ms
02-01 22:39:52.139: D/Facebook-WebView(637): Webview loading URL: https://m.facebook.com/dialog/oauth?display=touch&client_id=228461823905169&scope=user_likes%2Cread_stream%2Cuser_photos%2Cemail%2Cphoto_upload%2Coffline_access%2Cpublish_actions&type=user_agent&redirect_uri=fbconnect%3A%2F%2Fsuccess
02-01 22:40:12.209: D/Facebook-authorize(637): Login failed: com.facebook.android.DialogError: The connection to the server was unsuccessful.
02-01 22:40:12.279: D/Facebook-WebView(637): Webview loading URL: https://m.facebook.com/dialog/oauth?display=touch&client_id=228461823905169&scope=user_likes%2Cread_stream%2Cuser_photos%2Cemail%2Cphoto_upload%2Coffline_access%2Cpublish_actions&type=user_agent&redirect_uri=fbconnect%3A%2F%2Fsuccess
02-01 22:46:14.119: W/KeyCharacterMap(637): No keyboard for id 0
02-01 22:46:14.119: W/KeyCharacterMap(637): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
Я очистил кеш браузера и данные приложения.Я проверил хэш ключа и несколько раз скопировал и вставил идентификатор приложения в приложение.