简体   繁体   中英

Unity Facebook SDK: FB.Feed not posting

I finished my game. Now I want to make it social. Posting to twitter is easy, but I have a problem with facebook.

I created an app on facebook dev, I have key-hash, key-store, I don't have any errors in console. I made enviroment variables with paths to JDK and OpenSSL. Maybe there's something wrong with my code (it's from SDK example) but I don't know what.

This script is attached to NGUI button. In editor it works but on device it only asks to login and permission. Then nothing happens.

using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

public sealed class FacebookShare : MonoBehaviour {
#region FB.Init() example

private bool isInit = false;

void Awake ()
{
    CallFBInit();
}

private void CallFBInit()
{
    FB.Init(OnInitComplete, OnHideUnity);
}

private void OnInitComplete()
{
    Debug.Log("FB.Init completed: Is user logged in? " + FB.IsLoggedIn);
    isInit = true;
}

private void OnHideUnity(bool isGameShown)
{
    Debug.Log("Is game showing? " + isGameShown);
}

#endregion

#region FB.Login() example

private void CallFBLogin()
{
    FB.Login("email,publish_actions", LoginCallback);
}

void LoginCallback(FBResult result)
{
    if (result.Error != null)
        lastResponse = "Error Response:\n" + result.Error;
    else if (!FB.IsLoggedIn)
    {
        lastResponse = "Login cancelled by Player";
    }
    else
    {
        lastResponse = "Login was successful!";
    }
}

private void CallFBLogout()
{
    FB.Logout();
}
#endregion

#region FB.Feed() example

public string FeedToId = "";
public string FeedLink = "";
public string FeedLinkName = "";
public string FeedLinkCaption = "";
public string FeedLinkDescription = "";
public string FeedPicture = "";
public string FeedMediaSource = "";
public string FeedActionName = "";
public string FeedActionLink = "";
public string FeedReference = "";
public bool IncludeFeedProperties = false;
private Dictionary<string, string[]> FeedProperties = new Dictionary<string, string[]>();

private void CallFBFeed()
{
    Dictionary<string, string[]> feedProperties = null;
    if (IncludeFeedProperties)
    {
        feedProperties = FeedProperties;
    }
    FB.Feed(
        toId: FeedToId,
        link: FeedLink,
        linkName: FeedLinkName,
        linkCaption: FeedLinkCaption,
        linkDescription: FeedLinkDescription,
        picture: FeedPicture,
        mediaSource: FeedMediaSource,
        actionName: FeedActionName,
        actionLink: FeedActionLink,
        reference: FeedReference,
        properties: feedProperties,
        callback: Callback
        );
}

#endregion
public string ApiQuery = "";
private string lastResponse = "";
private Texture2D lastResponseTexture;

void Callback(FBResult result)
{
    lastResponseTexture = null;
    // Some platforms return the empty string instead of null.
    if (!String.IsNullOrEmpty(result.Error))
        lastResponse = "Error Response:\n" + result.Error;
    else if (!ApiQuery.Contains("/picture"))
        lastResponse = "Success Response:\n" + result.Text;
    else
    {
        lastResponseTexture = result.Texture;
        lastResponse = "Success Response:\n";
    }
}

void OnClick ()
{
    CallFBLogin();
    CallFBFeed();
}

}

You trying to feed before LoginCallback called. Try to change your LoginCallback and OnClick functions something like this:

   void LoginCallback(FBResult result) {
        if (result.Error != null)
            lastResponse = "Error Response:\n" + result.Error;
        else if (!FB.IsLoggedIn)
        {
            lastResponse = "Login cancelled by Player";
        }
        else
        {
            lastResponse = "Login was successful!";
            CallFBFeed();
        }
    }
    void OnClick ()
    {
       CallFBLogin();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM