Android上的Facebook Graph API - 在我自己的页面上创建新帖子

时间:2016-09-29 13:32:01

标签: java android facebook-graph-api facebook-android-sdk

我有这个源代码:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import java.net.URL;

import facebook4j.Account;
import facebook4j.Facebook;
import facebook4j.FacebookFactory;
import facebook4j.PostUpdate;
import facebook4j.auth.AccessToken;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);




        try{
        Facebook facebook = new FacebookFactory().getInstance();
        facebook.setOAuthAppId(String.valueOf(R.string.facebook_app_id), String.valueOf(R.string.facebook_app_secret));

         facebook.setOAuthAccessToken(new facebook4j.auth.AccessToken("10000000000003|mqs000000000000C46A"));

        String pageToken = "EAAUB000000000000000000000000000000000000000000000123000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000AZDZD";

        for (Account a : facebook.getAccounts()) {
            if (a.getName().toLowerCase().contains("nameOfPage")) {

                pageToken = a.getAccessToken();


                PostUpdate post = new PostUpdate(new URL("https://facebook.com/1000000000000003/feed"))
                        .picture(new URL("http://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-icon.png"))
                        .name("example name")
                        .caption("example caption")
                        .message("example message")
                        .description("example description");

                if (pageToken != null) {
                    facebook.setOAuthAccessToken(new AccessToken("EAAUB000000000000000000000000000000000000000000000000000123000000000000000000000000000000000000000000123000000000000000000000000000000000000000000000000000000000000000000000AZDZD"));
                    facebook.postFeed(post);
                    //ScriptGroup.Input.addInfoAnnotation(req, "sysAdminTools.annotation.fb.ok");
                }

            }


        }
    }catch(Exception e){
System.out.println();
        }
    }
}

使用清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="test.app">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
        <provider android:authorities="com.facebook.app.FacebookContentProvider1400000000000003"
            android:name="com.facebook.FacebookContentProvider"
            android:exported="true" />
    </application>
    <uses-permission android:name="android.permission.INTERNET" />
</manifest>

它不起作用。我试图谷歌它超过一个小时,我找不到任何期待this。我有自己生成的PageToken,AppToken和AppSecret。你能解释一下我的错误并推荐sollution吗?

每次按下按钮时,logcat都会显示,但没有任何反应:

09-11 8:24:69.420 28087-28087/test.app I/HwSecImmHelper: mSecurityInputMethodService is null

应用程序没有崩溃,没有抛出任何错误,根本没有发生任何事情。

由于

0 个答案:

没有答案