Example #1
0
        // Step 1 in https://developer.twitter.com/en/docs/media/upload-media/overview
        public async UniTask <TWMediaUploadInitResponse> InitMediaUpload(int byteSize, string mediaType, string mediaCategory)
        {
            var endpoint = UploadEndpoint;

            var form = new Dictionary <string, string>
            {
                { "command", "INIT" },
                { "total_bytes", $"{byteSize}" },
                { "media_type", mediaType },
                { "media_category", mediaCategory },
            };

            using (var req = FormatUtils.RequestPost(endpoint, form))
            {
                string authHeader = await MakeAuthHeaderAsUser("POST", endpoint, form);

                req.SetRequestHeader("Authorization", authHeader);

                await req.SendWebRequest();

                TWHttpException.ThrowIfError(req);

                string res = req.downloadHandler.text;
                return(JsonConvert.DeserializeObject <TWMediaUploadInitResponse>(res));
            }
        }
Example #2
0
        // POST with OAuth header prepared. You should be able to implement majority of POST APIs with this method
        // You MUST have authorized as an app or user earlier or you'll receive 401 error
        public async UniTask <T> Post <T>(string path, IDictionary <string, string> form)
        {
            var endpoint = $"{ApiEndpoint}/{path}.json";

            using (var req = FormatUtils.RequestPost(endpoint, form))
            {
                string authHeader = await MakeAuthHeaderAsUser("POST", endpoint, form);

                req.SetRequestHeader("Authorization", authHeader);

                await req.SendWebRequest();

                TWHttpException.ThrowIfError(req);

                string res = req.downloadHandler.text;
                return(JsonConvert.DeserializeObject <T>(res));
            }
        }
Example #3
0
        // https://developer.twitter.com/en/docs/media/upload-media/api-reference/get-media-upload-status
        public async UniTask <TWMediaUploadStatusResponse> GetMediaUploadStatus(string mediaId)
        {
            var endpoint = UploadEndpoint;

            var form = new Dictionary <string, string>
            {
                { "command", "STATUS" },
                { "media_id", mediaId },
            };

            using (var req = UnityWebRequest.Get($"{endpoint}?{FormatUtils.UrlEncode(form)}"))
            {
                string authHeader = await MakeAuthHeaderAsUser("GET", endpoint, form);

                req.SetRequestHeader("Authorization", authHeader);

                await req.SendWebRequest();

                TWHttpException.ThrowIfError(req);

                string res = req.downloadHandler.text;
                return(JsonConvert.DeserializeObject <TWMediaUploadStatusResponse>(res));
            }
        }
Example #4
0
        // Step 3 in https://developer.twitter.com/en/docs/media/upload-media/overview
        public async UniTask <TWMediaUploadFinalizeResponse> FinalizeMediaUpload(string mediaId)
        {
            var endpoint = UploadEndpoint;

            var form = new Dictionary <string, string>
            {
                { "command", "FINALIZE" },
                { "media_id", mediaId },
            };

            using (var req = FormatUtils.RequestPost(endpoint, form))
            {
                string authHeader = await MakeAuthHeaderAsUser("POST", endpoint, form);

                req.SetRequestHeader("Authorization", authHeader);

                await req.SendWebRequest();

                TWHttpException.ThrowIfError(req);

                string res = req.downloadHandler.text;
                return(JsonConvert.DeserializeObject <TWMediaUploadFinalizeResponse>(res));
            }
        }