protected void TryLogin(string apiPath, string password) { apiPath = apiPath.Trim(); if (apiPath[apiPath.Length - 1] == '/') { apiPath = apiPath.Remove(apiPath.Length - 1); } var request = UnityWebRequest.Get(apiPath + "/user"); request.SetRequestHeader("PRIVATE-TOKEN", password); var async = request.SendWebRequest(); async.completed += op => { UnityWebRequestAsyncOperation asyncop = op as UnityWebRequestAsyncOperation; if (asyncop.webRequest.isHttpError) { Debug.Log(asyncop.webRequest.error + " :: " + asyncop.webRequest.downloadHandler.text); GetAuthToken(); } else { GitlabUserData user = JsonUtility.FromJson <GitlabUserData>(asyncop.webRequest.downloadHandler.text); _userID = user.id; var setting = BugReporterPlugin.settings.GetBackendSettings(backendName); setting.token = password; setting.apiPath = apiPath; _apiPath = apiPath; _token = password; _isLoggedIn = true; BugReporterPlugin.SaveSettings(); Debug.LogFormat("[Gitlab Backend] : Logged in as user " + user.name); // Gitlab have an image uploader setup by default BugReporterPlugin.SetupImageUploader(GitlabImageUploader.imgUploaderName); if (setting.projectPath != "") { SetupProjectInfo(); } if (OnPostInit != null) { OnPostInit.Invoke(); } } }; }
protected void TryLogin(string password) { var request = UnityWebRequest.Get(baseURL + "/user"); request.SetRequestHeader("Accept", "application/vnd.github.v3+json"); request.SetRequestHeader("Authorization", "token " + password); var async = request.SendWebRequest(); async.completed += op => { UnityWebRequestAsyncOperation asyncop = op as UnityWebRequestAsyncOperation; if (asyncop.webRequest.isHttpError) { Debug.Log(asyncop.webRequest.error); GetAuthToken(); } else { string response = asyncop.webRequest.GetResponseHeader("X-OAuth-Scopes"); if (response.Contains("repo")) { GithubUserData user = JsonUtility.FromJson <GithubUserData>(asyncop.webRequest.downloadHandler.text); _userID = user.id; var setting = BugReporterPlugin.settings.GetBackendSettings(backendName); setting.token = password; _token = password; _isLoggedIn = true; BugReporterPlugin.SaveSettings(); Debug.LogFormat("[Github Backend] : Logged in as user " + user.login); if (setting.projectPath != "") { SetupProjectInfo(); } if (OnPostInit != null) { OnPostInit.Invoke(); } } else { Debug.LogError("[Github Backend] : This Personal Access token don't have the repo access"); GetAuthToken(); } } }; }
public override void UploadFile(byte[] data, System.Action <bool, string> onUploadFinished) { var settings = BugReporterPlugin.settings.GetImageUploaderSetting(_uploaderName); if (settings.authentification == "") { var tokenEnter = EditorWindow.GetWindow <ImgurTokenEntry>(); tokenEnter.onEntered = win => { settings.authentification = win.appID; BugReporterPlugin.SaveSettings(); InternalUpload(data, onUploadFinished); }; } else { InternalUpload(data, onUploadFinished); } }
void InternalUpload(byte[] data, System.Action <bool, string> onUploadFinished) { var request = new UnityWebRequest("https://api.imgur.com/3/image", UnityWebRequest.kHttpVerbPOST); request.SetRequestHeader("Authorization", "Client-ID 7b2138351689000"); request.uploadHandler = new UploadHandlerRaw(data); request.uploadHandler.contentType = "image/png"; request.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer(); var async = request.SendWebRequest(); async.completed += op => { UnityWebRequestAsyncOperation asyncop = op as UnityWebRequestAsyncOperation; if (asyncop.webRequest.isHttpError) { Debug.LogErrorFormat("[Imgur Uploader] Error {0} : {1}", asyncop.webRequest.responseCode, asyncop.webRequest.error); if (asyncop.webRequest.responseCode == 401) {//unauthorized error, the access token is probably invalid, so remove it from the settings. var settings = BugReporterPlugin.settings.GetImageUploaderSetting(_uploaderName); settings.authentification = ""; BugReporterPlugin.SaveSettings(); } onUploadFinished(false, ""); } else { DataWrapper <ImgurUploadResponse> response = JsonUtility.FromJson <DataWrapper <ImgurUploadResponse> >(asyncop.webRequest.downloadHandler.text); onUploadFinished(true, response.data.link); } }; }