gpt4 book ai didi

Android:使用 FB SDK 4.x 以编程方式发布到 facebook

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:53:55 24 4
gpt4 key购买 nike

我想在没有对话的情况下发布到 facebook,只是以编程方式,我正在尝试

public void login(CallbackManager callbackManager) {
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.d(LOGTAG, "facebook login success");
share("kk");
}

@Override
public void onCancel() {
Log.w(LOGTAG, "facebook login canceled");
share("kk");
}

@Override
public void onError(FacebookException exception) {
Log.e(LOGTAG, "facebook login error");
exception.printStackTrace();
}
});

LoginManager.getInstance().logInWithPublishPermissions((Activity) context, Arrays.asList("publish_actions"));
}

public void share(final String msg) {

Log.d(LOGTAG, "facebook sharing new message");
Set<String> permissions = AccessToken.getCurrentAccessToken().getPermissions();
AccessToken accessToken = AccessToken.getCurrentAccessToken();

Bundle postParams = new Bundle();
postParams.putString("name", "yourTitle");
postParams.putString("caption", "yourCaption");
postParams.putString("description", "yourDesc");
postParams.putString("message", "yourMessage");

GraphRequest request = new GraphRequest(accessToken, "me/feed", postParams, HttpMethod.POST, null);
AsynTaskGraphRequest asynTaskGraphRequest = new AsynTaskGraphRequest(request);
asynTaskGraphRequest.execute();
}

问题是,当应用程序登录到 facebook 时,它会显示消息“这不允许应用程序发布到 facebook”,尽管我正在使用

LoginManager.getInstance().logInWithPublishPermissions((Activity) context, Arrays.asList("publish_actions"));

enter image description here

另一方面,如何检查用户是否仍然登录 fb 以仅调用一次 logInWithPublishPermissions?

谢谢!

最佳答案

正如@Ankush 所说,必须在开发人员 facebook 网站的应用程序设置中注册为授权开发人员。

我的最终代码(现在工作正常)是:

public class FbManager {

private Context context;
private static final String LOGTAG = "FbManager";
private CallbackManager callbackManager;

public FbManager(Context context, CallbackManager callbackManager){
this.context = context;
this.callbackManager = callbackManager;
}

public static void traceKeyHash(Activity activity){
try {
PackageInfo info = activity.getPackageManager().getPackageInfo("com.muv.android", PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
Log.i(LOGTAG, "Share - KeyHash: " + Base64.encodeToString(md.digest(), Base64.DEFAULT));
}
}
catch (Exception e) {
e.printStackTrace();
}
}

public void share(final String msg) {

if (isLoggedIn()) {
post(msg);
}
else{
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.d(LOGTAG, "facebook login success");
post(msg);
}

@Override
public void onCancel() {
Log.w(LOGTAG, "facebook login canceled");
}

@Override
public void onError(FacebookException exception) {
Log.e(LOGTAG, "facebook login error");
exception.printStackTrace();
}
});

LoginManager.getInstance().logInWithPublishPermissions((Activity) context, Arrays.asList("publish_actions"));
}
}

public boolean isLoggedIn() {
AccessToken accessToken = AccessToken.getCurrentAccessToken();
return accessToken != null;
}

private void post(final String msg){
Log.d(LOGTAG, "facebook posting new message");
Set<String> permissions = AccessToken.getCurrentAccessToken().getPermissions();
AccessToken accessToken = AccessToken.getCurrentAccessToken();

Bundle postParams = new Bundle();
postParams.putString("message", msg);

GraphRequest request = new GraphRequest(accessToken, "me/feed", postParams, HttpMethod.POST, null);
GraphRequestAsyncTask asynTaskGraphRequest = new GraphRequestAsyncTask (request);
asynTaskGraphRequest.execute();
}
}

关于Android:使用 FB SDK 4.x 以编程方式发布到 facebook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31273675/

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