C# 从Android共享到Facebook
我从android使用unity C#与facebook分享了一些问题 当我尝试、调试时,总是需要用户访问令牌来共享,而当共享时,它只是共享给测试人员和管理员 当我在设备上试用时,它只是显示“此应用程序将访问您的电子邮件…”,然后什么也没发生 这是我的代码:C# 从Android共享到Facebook,c#,android,facebook,unity3d,C#,Android,Facebook,Unity3d,我从android使用unity C#与facebook分享了一些问题 当我尝试、调试时,总是需要用户访问令牌来共享,而当共享时,它只是共享给测试人员和管理员 当我在设备上试用时,它只是显示“此应用程序将访问您的电子邮件…”,然后什么也没发生 这是我的代码: using UnityEngine; using System; using System.Collections; using System.Collections.Generic; using System.Linq; using Fa
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Facebook;
public class PTShare : MonoBehaviour {
private string lastResponse = "";
public OTSprite shareBtn;
// Use this for initialization
void Awake () {
CallFBInit ();
}
void Start () {
shareBtn = GameObject.Find ("share").GetComponent<OTSprite>();
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown (0)) {
if(OT.Over(shareBtn)) {
FB.Login("email,publish_actions", LoginCallback);
CallFBFeed();
}
}
}
public static IEnumerator GetAppAccessToken() {
//Debug.Log("asking FB for App AccessToken");
string url = string.Format("https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_ id={0}&client_secret={1}",
"1443575049241638",
"efde01d85ba62e4d81708b4189419d5c");
WWW fbRequest = new WWW(url);
// Wait for download to complete
yield return fbRequest;
string accessToken = fbRequest.text;
Debug.Log("got token: " + accessToken);
accessToken = accessToken.Substring(accessToken.IndexOf("=") + 1);
Debug.Log("trimmed token: " + accessToken);
//FB.UserId = accessToken;
return true;
}
public string FeedToId = "";
public string FeedLink = "http://gambrenggames.com/game-detail/junes-potion/2";
public string FeedLinkName = "June's Potion";
public string FeedLinkCaption = "I play June's Potion friends! Can you play it?";
public string FeedLinkDescription = "";
public string FeedPicture = "http://gambrenggames.com/website/images/game-icon/05540225032014.png";
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
);
}
private Texture2D lastResponseTexture;
public string ApiQuery = "";
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";
}
}
private bool isInit = false;
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);
}
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();
}
}
使用UnityEngine;
使用制度;
使用系统集合;
使用System.Collections.Generic;
使用System.Linq;
使用Facebook;
公共类PTShare:单一行为{
私有字符串lastResponse=“”;
公共OTSprite共享BTN;
//用于初始化
无效唤醒(){
CallFBInit();
}
无效开始(){
shareBtn=GameObject.Find(“share”).GetComponent();
}
//每帧调用一次更新
无效更新(){
if(Input.GetMouseButtonDown(0)){
如果(加班结束(共享BTN)){
FB.登录(“电子邮件、发布操作”,LoginCallback);
CallFBFeed();
}
}
}
公共静态IEnumerator GetAppAccessToken(){
//Log(“向FB请求应用访问令牌”);
字符串url=string.Format(“https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_ id={0}&客户端_secret={1}“,
"1443575049241638",
“efde01d85ba62e4d81708b4189419d5c”);
WWW fbRequest=新的WWW(url);
//等待下载完成
提出退货要求;
字符串accessToken=fbRequest.text;
Log(“获取令牌:+accessToken”);
accessToken=accessToken.Substring(accessToken.IndexOf(“=”)+1);
Log(“修剪标记:+accessToken”);
//FB.UserId=accessToken;
返回true;
}
公共字符串FeedToId=“”;
公共字符串FeedLink=”http://gambrenggames.com/game-detail/junes-potion/2";
公共字符串FeedLinkName=“六月的药水”;
public string FeedLinkCaption=“我玩琼的魔药朋友!你能玩吗?”;
公共字符串FeedLinkDescription=“”;
公共字符串FeedPicture=”http://gambrenggames.com/website/images/game-icon/05540225032014.png";
公共字符串FeedMediaSource=“”;
公共字符串FeedActionName=“”;
公共字符串FeedActionLink=“”;
公共字符串FeedReference=“”;
public bool IncludeFeedProperties=false;
私有字典FeedProperties=新字典();
私有void CallFBFeed()
{
Dictionary feedProperties=null;
if(IncludeFeedProperties)
{
feedProperties=feedProperties;
}
饲料(
toId:FeedToId,
链接:FeedLink,
linkName:FeedLinkName,
linkCaption:FeedLinkCaption,
linkDescription:FeedLinkDescription,
图片:FeedPicture,
mediaSource:FeedMediaSource,
actionName:FeedActionName,
actionLink:FeedActionLink,
参考:FeedReference,
属性:feedProperties,
回调:回调
);
}
私有纹理2D lastResponseTexture;
公共字符串ApiQuery=“”;
无效回调(FBResult结果)
{
lastResponseTexture=null;
//有些平台返回空字符串而不是null。
如果(!String.IsNullOrEmpty(result.Error))
lastResponse=“错误响应:\n”+结果。错误;
如果(!ApiQuery.Contains(“/picture”),则为else
lastResponse=“成功响应:\n”+结果.Text;
其他的
{
lastResponseTexture=result.Texture;
lastResponse=“成功响应:\n”;
}
}
private bool isInit=false;
私有void CallFBInit()
{
FB.Init(OnInitComplete,OnHideUnity);
}
私有void OnInitComplete()
{
Debug.Log(“FB.Init完成:用户是否登录?”+FB.IsLoggedIn);
isInit=true;
}
私密的虚空隐藏(如图所示)
{
Log(“正在播放游戏吗?”+正在播放游戏);
}
私有void CallFBLogin()
{
FB.登录(“电子邮件、发布操作”,LoginCallback);
}
void LoginCallback(FBResult结果)
{
if(result.Error!=null)
lastResponse=“错误响应:\n”+结果。错误;
如果(!FB.IsLoggedIn)为else,则为
{
lastResponse=“玩家取消登录”;
}
其他的
{
lastResponse=“登录成功!”;
}
}
私有void CallFBLogout()
{
FB.Logout();
}
}
FB.Login是异步方法,FB.Feed(可能?)需要登录
因此,尝试调用CallFBFeed();内部LoginCallback方法。就像这样:
void Update () {
if (Input.GetMouseButtonDown (0)) {
if(OT.Over(shareBtn)) {
if (FB.IsLoggedIn)
{
CallFBFeed();
}
else
{
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!";
CallFBFeed();
}
}
但这可能是android设备的问题(我在一些旧设备上使用了“魔法”fb登录)或设置,并且您的代码还可以