Translate

2015年12月10日 星期四

Facebook Android SDK 4 串接(二) - 個人資訊及發文

繼上篇Facebook Android SDK 4 串接(一) - Login 取Token

拿到Token後,用來讀取個人資訊及發文。

個人資訊:
loginButton.setReadPermissions("user_birthday");
要讀取生日須先取得權限。
 ※權限表Permissions

GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() 
            {
                @Override
                public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) {
                    Log.d("Show", jsonObject.toString());                    
                }
            });
            Bundle parameters = new Bundle();
            parameters.putString("fields", "id,name,birthday,gender,link,picture");
            request.setParameters(parameters);
            request.executeAsync();

取得的JSON(因為個資我就用"*"替代):
{
  "id": "8995768600*****",
  "name": "林**",
  "birthday": "11/29/1991",
  "gender": "male",
  "link": "https://www.facebook.com/app_scoped_user_id/8995768600*****/",
  "picture": {
    "data": {
      "is_silhouette": false,
      "url": "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xat1/v/t1.0-1/p50x50/12107037_1222898491060696_69373362222982*****_n.jpg?oh=729d711562d4315ba7cd03e72da693f2&oe=56E7F4B2&__gda__=1457302430_b22f949093879b5ccc2fa7b251c286cc"
    }
  }
}
接下來就解JSON就可以取得個別資訊了。

設定大頭貼:
使用SDK元件呈現,也可用個人資訊的picture{url}來呈現,但像素較低。
<com.facebook.login.widget.ProfilePictureView
    android:id="@+id/selection_profile_pic"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"/>
profilePictureView = (ProfilePictureView) findViewById(R.id.selection_profile_pic);
profilePictureView.setCropped(true);
profilePictureView.setPresetSize(ProfilePictureView.NORMAL);
profilePictureView.setProfileId(id);


發文:
Facebook Android SDK4.0+則是用ShareDialog來取代發文。
Sharing on Android:https://developers.facebook.com/docs/sharing/android
我們直接來看Code吧!
這是最簡單的發文(純文字)。
ShareDialog shareDialog = new ShareDialog(MainActivity.this);
ShareLinkContent linkContent = new ShareLinkContent.Builder().build();
shareDialog.show(linkContent);












假如你想對發文添加一些內容則在ShareLinkConten做一些設定。
ShareDialog shareDialog = new ShareDialog(MainActivity.this);
ShareLinkContent linkContent = new ShareLinkContent.Builder()
          .setContentTitle("Facebook SDK 4.0 Test")  //設定標題
          .setContentDescription("測試")  //設定內文
          .setContentUrl(Uri.parse("http://developers.facebook.com/android")) //設定連結
          .build();
shareDialog.show(linkContent);













發文配上動作(健身):

ShareOpenGraphObject object = new ShareOpenGraphObject.Builder()
                            .putString("og:type", "fitness.course")
                            .putString("og:title", "TestApp")
                            .putString("og:description", "利用此app紀錄")
                            .putInt("fitness:duration:value", 100)
                            .putString("fitness:duration:units", "s")
                            .putInt("fitness:distance:value", 12)
                            .putString("fitness:distance:units", "km")
                            .putInt("fitness:speed:value", 5)
                            .putString("fitness:speed:units", "m/s")
                            .build();
ShareOpenGraphAction action = new ShareOpenGraphAction.Builder()
                            .setActionType("fitness.runs")
                            .putObject("fitness:course", object)
                            .build();
ShareOpenGraphContent content = new ShareOpenGraphContent.Builder()
                            .setPreviewPropertyName("profile")
                            .setAction(action)
                            .build();
shareDialog.show(content);















可使用的Actions請參照:https://developers.facebook.com/docs/sharing/opengraph/using-actions

在做好友清單卡好久,response 的data一直為null,百思不得其解,餵google後得知Graph API 2.0 之後就無法取得,只能取到也同意你這app的user_friends權限的好友,因此null的原因是你好友並沒有受權給此app的人。

沒有留言:

張貼留言