gpt4 book ai didi

android - com.facebook.FacebookException : Attempted to use a Session that was not open

转载 作者:太空宇宙 更新时间:2023-11-03 12:34:17 25 4
gpt4 key购买 nike

我集成了 facebook 共享,前几天运行良好,但今天就在问这个问题前 2 小时,我遇到了这个问题。

我正在使用此代码进行登录:

  Session.openActiveSession(this, true, new Session.StatusCallback() {

// callback when session changes state
@Override
public void call(Session session, SessionState state, Exception exception) {
if (state.isOpened()) {

// make request to the /me API
Request.newMeRequest(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(ImagePagerActivity.this, user.getName()+" Logged In...", Toast.LENGTH_LONG).show();
}
}
}).executeAsync();
}
}
});
publishFeedDialog();

问题出在 if() 条件中,它始终为 false,而且我使用了 session.isOpened() 也返回 false,我很困惑为什么会这样。

我已经在 list 中声明了 INTERNET、ACCESSNETWORKSTATE、权限

 <application>
..........
..........
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/app_id"/>
</application>

请帮忙...

编辑

这是我实现的代码,你可以看到onCreateOptionsMenuonOptionsItemSelectedonActivityResultpublishFeedDialog

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.action_bar_share_menu, menu);
MenuItem item = menu.findItem(R.id.menu_item_share1);


return true;
}
@Override

public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_share1: // facebook item selected
Toast.makeText(this, "Menu Item 1 selected", Toast.LENGTH_SHORT).show();
//start Facebook Login


Session.openActiveSession(this, true, new Session.StatusCallback() {

// callback when session changes state
@Override
public void call(Session session, SessionState state, Exception exception) {
if (state.isOpened()) {

// make request to the /me API
Request.newMeRequest(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(ImagePagerActivity.this, user.getName()+" Logged In...", Toast.LENGTH_LONG).show();
}
}
}).executeAsync();
}
}
});
publishFeedDialog();

}
return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

uiHelper.onActivityResult(requestCode, resultCode, data, new FacebookDialog.Callback() {
@Override
public void onError(FacebookDialog.PendingCall pendingCall, Exception error, Bundle data) {
Log.e("Activity", String.format("Error: %s", error.toString()));
}

@Override
public void onComplete(FacebookDialog.PendingCall pendingCall, Bundle data) {
Log.i("Activity", "Success!");
}
});

}
@Override
protected void onResume() {
super.onResume();
uiHelper.onResume();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putInt(STATE_POSITION, pager.getCurrentItem());
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
}

@Override
public void onPause() {
super.onPause();
uiHelper.onPause();
}

@Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}

private void publishFeedDialog() {

Log.v("pos",""+pos);
Bundle params = new Bundle();
params.putString("name", ItemListApplication.names.get(pos).getItem());
params.putString("caption", "Irrational beliefs");
params.putString("description",ItemListApplication.names.get(pos).getDesc() );
params.putString("link", "https://www.facebook.com/vivek.warde.3?ref=tn_tnmn");

if(pos==0) params.putString("picture","http://i.imgur.com/lOIUcW2.jpg");
else params.putString("picture",imageUrls[pos]);
WebDialog feedDialog = (
new WebDialog.FeedDialogBuilder(this,
Session.getActiveSession(),
params))
.setOnCompleteListener(new OnCompleteListener() {

@Override
public void onComplete(Bundle values,
FacebookException error) {
if (error == null) {
// When the story is posted, echo the success
// and the post Id.
final String postId = values.getString("post_id");
if (postId != null) {
Toast.makeText(ImagePagerActivity.this,
"Posted story, id: "+postId,
Toast.LENGTH_SHORT).show();
} else {
// User clicked the Cancel button
Toast.makeText(ImagePagerActivity.this,
"Publish cancelled",
Toast.LENGTH_SHORT).show();
}
} else if (error instanceof FacebookOperationCanceledException) {
// User clicked the "x" button
Toast.makeText(ImagePagerActivity.this,
"Publish cancelled",
Toast.LENGTH_SHORT).show();
} else {
// Generic, ex: network error
Toast.makeText(ImagePagerActivity.this,
"Error posting story",
Toast.LENGTH_SHORT).show();
}
}

})
.build();
feedDialog.show();
}

最佳答案

处理这个问题可能有很多原因。你应该检查其中的一些:

我正在使用此代码通过 facebook 登录:

class MyFragment extends Fragment {
//... some code

SessionStatusCallback statusCallback = new SessionStatusCallback();

public void login() {
Session session = Session.getActiveSession();
if (!session.isOpened() && !session.isClosed()) {
session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
} else {
Session.openActiveSession(getActivity(), this, true, statusCallback);
}
}

private class SessionStatusCallback implements Session.StatusCallback {
@Override
public void call(Session session, SessionState state, Exception exception) {
if (exception != null) {
handleException(exception);
}
if (state.isOpened()) {
afterLogin();
} else if (state.isClosed()) {
afterLogout();
}
}
}

}

如果 session 未打开,您应该打开它以供阅读(如果需要,也可以打开 publish)。


检查AndroidManifest文件中的meta-data标签,是否有正确的application id?


经常发生错误不是在 android 应用程序中,而是在 facebook 应用程序设置中。在 facebook 应用程序的设置页面中应该是正确的包名称,并且您应该为您的应用程序添加 key 哈希(每种发布类型都不同,在大多数情况下是调试和发布)。

facebook android settings

要获取哈希键,您可以运行脚本

keytool -exportcert -alias YOUR_ALIAS -keystore PATH_TO_KEYSTORE_FILE | openssl sha1 -binary | openssl base64

或者你可以在代码中获取它:

PackageInfo info;
try {
info = getPackageManager().getPackageInfo("YOUR_APP_PACKAGE", PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest md;
md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
String keyhash = new String(Base64.encode(md.digest(), 0));
//string something is what you should paste as key hash
Log.e("hash key", keyhash);
}
} catch (NameNotFoundException e1) {
Log.e("name not found", e1.toString());
} catch (NoSuchAlgorithmException e) {
Log.e("no such an algorithm", e.toString());
} catch (Exception e) {
Log.e("exception", e.toString());
}

在 Facebook 应用程序的设置页面中,您应该将应用程序公开。或者,如果您仍然不想将其公开,您应该为所有可以使用您的 Android 应用程序的用户添加角色(在“角色”选项卡中)。


另外,如果没有帮助,尝试调试方法
public void call(Session session ,SessionState状态,Exception异常)
经常出现授权不成功的正常提示

关于android - com.facebook.FacebookException : Attempted to use a Session that was not open,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24656830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com