// 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)); } }
// 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)); } }
// 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)); } }
// 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)); } }