我正在尝试使用Facebook sdk登录Facebook。但它没有登录。它打开对话框但在获取凭据后它无法正常工作。如果我安装Facebook应用程序它打开Facebook应用程序但登录未完成我无法获得令牌和用户ID。我也无法将信息发布到墙上。但它显示“消息成功显示”的吐司,但它没有张贴在Facebook墙上。 代码:
public boolean isSession() {
access_token = sp.getString(TOKEN, "x");
expires = sp.getLong(EXPIRES, -1);
Log.d("TAG", access_token);
if (access_token != null && expires != -1) {
facebook.setAccessToken(access_token);
facebook.setAccessExpires(expires);
}
return facebook.isSessionValid();
}
private class LoginDialogListener implements DialogListener {
@Override
public void onComplete(Bundle values) {
Log.d("TAG", "LoginONComplete");
token =facebook.getAccessToken();
token_expires = facebook.getAccessExpires();
Log.d("TAG", "AccessToken: " + token);
Log.d("TAG", "AccessExpires: " + token_expires);
savePrefs3(EXPIRES,token_expires);
savePrefs(TOKEN,token);
mAsyncRunner.request("me", new IDRequestListener());
}
@Override
public void onFacebookError(FacebookError e) {
Log.d("TAG", "FacebookError: " + e.getMessage());
}
@Override
public void onError(DialogError e) {
Log.d("TAG", "Error: " + e.getMessage());
}
@Override
public void onCancel() {
Log.d("TAG", "OnCancel");
}
}
private class IDRequestListener implements RequestListener {
@Override
public void onComplete(String response, Object state) {
try {
Log.d("TAG", "IDRequestONComplete");
Log.d("TAG", "Response: " + response.toString());
JSONObject json = Util.parseJson(response);
Uid = json.getString("id");
savePrefs("UID", Uid);
final String name = json.getString("name");
} catch (JSONException e) {
Log.d("TAG", "JSONException: " + e.getMessage());
} catch (FacebookError e) {
Log.d("TAG", "FacebookError: " + e.getMessage());
}
}
@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
}
}
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.");
} else {
showToast("Message posted to your facebook wall!");
}
finish();
} catch (Exception e) {
howToast("Failed to post to wall!");
e.printStackTrace();
finish();
}
}
private void showToast(String message) {
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
}
请告诉我我哪里出错了。显示一个Toast应用程序后关闭。当我触摸屏幕时加载fb对话框,它会重新加载或切换回应用程序窗口。
请立即回复
答案 0 :(得分:4)
Facebook对象已弃用。您可以在活动 oncreate()方法中使用以下代码。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
// setContentView(R.layout.facebook_dialog);
Session.openActiveSession(this, true, new Session.StatusCallback() {
// callback when session changes state
@Override
public void call(Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
// make request to the /me API
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
// callback after Graph API response with user object
@Override
public void onCompleted(GraphUser user, Response response) {
if (user == null) {
Toast.makeText(LoginFacebook.this.getApplicationContext(),
"Facebook Error",
Toast.LENGTH_LONG).show();
finish();
}
else
{
Toast.makeText(LoginFacebook.this.getApplicationContext(),
user.getName()+" Logged in Successfully.",
T
Toast.LENGTH_LONG).show();
finish();
}
//login_fb.setEnabled(true);
//progress.setVisibility(View.INVISIBLE);
}
});
}
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
finish();
}
并在应用程序标记下的 Manifest.xml 文件中使用以下代码。
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/app_id" />
<activity
android:name="com.facebook.LoginActivity"
android:label="@string/app_name" >
</activity>
在登录发布到墙后,您必须调用此方法...
private void publishStory(String user) {
try {
Session session = Session.getActiveSession();
if (session != null){
// Check for publish permissions
List<String> permissions = session.getPermissions();
if (!isSubsetOf(PERMISSIONS, permissions)) {
pendingPublishReauthorization = true;
Session.NewPermissionsRequest newPermissionsRequest = new Session
.NewPermissionsRequest(this, PERMISSIONS);
session.requestNewPublishPermissions(newPermissionsRequest);
return;
}
Bundle postParams = new Bundle();
postParams.putString("message", messageToPost);
Request.Callback callback= new Request.Callback() {
public void onCompleted(Response response) {
JSONObject graphResponse = response
.getGraphObject()
.getInnerJSONObject();
String postId = null;
try {
postId = graphResponse.getString("id");
} catch (Exception e) {
Log.i("Test",
"JSON error "+ e.getMessage());
}
FacebookRequestError error = response.getError();
if (error != null) {
Toast.makeText(ShareOnFacebook.this
.getApplicationContext(),
error.getErrorMessage(),
Toast.LENGTH_SHORT).show();
} else {
progress.setVisibility(View.INVISIBLE);
toastmessage="Posted Successfully";
Toast.makeText(ShareOnFacebook.this
.getApplicationContext(),
toastmessage,
Toast.LENGTH_SHORT).show();
}
}
};
Request request = new Request(session, user+"/feed", postParams,
HttpMethod.POST, callback);
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
}
} catch (Exception e) {
// TODO Auto-generated catch block
Toast.makeText(ShareOnFacebook.this
.getApplicationContext(),
"Facebook Error",
Toast.LENGTH_SHORT).show();
}
}
private boolean isSubsetOf(Collection<String> subset, Collection<String> superset) {
for (String string : subset) {
if (!superset.contains(string)) {
return false;
}
}
return true;
}
和将变量声明为..
private static final List<String> PERMISSIONS = Arrays.asList("publish_actions","manage_pages","publish_stream");
private static final String PENDING_PUBLISH_KEY = "pendingPublishReauthorization";
private boolean pendingPublishReauthorization = false;
答案 1 :(得分:0)
不完全是您问题的答案,但如果您开始开发应用程序:在{3.} API中不推荐使用Facebook
对象。
在新API中,您应该使用Session
对象以及UiLifecycleHelper