簡體   English   中英

Unity3d將圖像上傳到Twitter

[英]Unity3d Upload a Image to Twitter

我可以登錄和發短信,但不能在Twitter上分享圖片。

當我上傳圖像時,它給出了響應,但是當我沒有在Twitter上發布圖像時,得到了以下響應。

這是我的代碼 :-

    private const string UploadMediaURL = "https://upload.twitter.com/1.1/media/upload.json";

    public static IEnumerator UploadMedia(string consumerKey, string consumerSecret, AccessTokenResponse response)
    {
        Dictionary<string, string> parameters = new Dictionary<string,string>();
        Texture2D tex= Resources.Load("imag") as Texture2D;
        byte[] dataToSave = tex.EncodeToPNG ();
        string encoded64ImageData = System.Convert.ToBase64String( dataToSave );
        parameters.Add("media_data",encoded64ImageData);
        WWWForm form = new WWWForm(); // Add data to the form to post.
        form.AddField("media_data", encoded64ImageData );
        Dictionary<string, string> headers = new Dictionary<string, string>();
        string auth = GetHeaderWithAccessToken("POST", UploadMediaURL, consumerKey, consumerSecret, response, parameters);
        Debug.Log ("Auth   " + auth);
        headers.Add( "Authorization", auth);
        headers.Add( "Content-Transfer-Encoding","base64");
        WWW web = new WWW(UploadMediaURL,form.data, headers);
        yield return web;
        Debug.Log ("Response  " + web.text);
    }

回應 :-

{
"media_id":700577726298599424,
"media_id_string":"700577726298599424",
"size":9734,
"expires_after_secs":86400,
"image":
        {
         "image_type":"image\/png",
         "w":435,
         "h":159
         }
}

這是我的Media_Id發布源代碼。 當我不帶media_id使用它時,它會成功發布。 但是當我給它一個media_id參數時,它給了我一個錯誤

失敗了 401未經授權{“錯誤”:[{“代碼”:32,“消息”:“無法驗證您的身份。”}]}

源代碼

    private const string PostTweetURL = "https://api.twitter.com/1.1/statuses/update.json";

    public static IEnumerator PostTweet(string text, string consumerKey, string consumerSecret, AccessTokenResponse response, PostTweetCallback callback)
    {
        if (string.IsNullOrEmpty(text) || text.Length > 140)
        {
            Debug.Log(string.Format("PostTweet - text[{0}] is empty or too long.", text));

            callback(false);
        }
        else
        {
            Dictionary<string, string> parameters = new Dictionary<string, string>();
            parameters.Add("status", text);

            WWWForm form = new WWWForm();
            form.AddField("status", text);
            form.AddField ("media_id", "732498072731713536");
            // HTTP header
            var headers = new Dictionary<string,string>();

            headers["Authorization"] = GetHeaderWithAccessToken("POST", PostTweetURL, consumerKey, consumerSecret, response, parameters);

            WWW web = new WWW(PostTweetURL, form.data, headers);
            yield return web;

            if (!string.IsNullOrEmpty(web.error))
            {
                Debug.Log(string.Format("PostTweet - failed. {0}\n{1}", web.error, web.text));
                callback(false);
            }
            else
            {
                string error = Regex.Match(web.text, @"<error>([^&]+)</error>").Groups[1].Value;

                if (!string.IsNullOrEmpty(error))
                {
                    Debug.Log(string.Format("PostTweet - failed. {0}", error));
                    callback(false);
                }
                else
                {
                    callback(true);
                }
            }
        }
    }

完全沒有出現在Twitter上是完全合理的。 當前提供的代碼僅用於將圖像上傳到twitter服務,然后可以將其附加到twitter狀態更新中。

例如,可以使用media_id值使用狀態/更新端點來創建帶有附帶照片的Tweet。 資源

因此,在上傳您的圖片並收到可在更新中使用的media_id

更新身份驗證用戶的當前狀態,也稱為Tweeting。

編輯

這是我的Media_Id發布源代碼。 當我不帶media_id使用它時,它會成功發布。 但是當我給它一個media_id參數時,它給了我一個錯誤

失敗了 401未經授權{“錯誤”:[{“代碼”:32,“消息”:“無法驗證您的身份。”}]}

您收到此錯誤,因為您沒有得到驗證。 由於錯誤本身也是如此。 更新文檔中所述, 上傳媒體時,您需要記住一些事情。 如此處所述,與普通推文不同,在計算OAuth簽名時不使用POST和查詢參數。

由於該方法使用多部分POST ,因此OAuth的處理方式略有不同。 計算OAuth簽名基本字符串或簽名時,不使用POST或查詢字符串參數。 僅使用oauth_ *參數。

為什么沒有任何推文對此認證有一些很好的答案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM