private async Task <GoogleOauthResponse> RequestToken(string authorizationString) { Debug.WriteLine("CODE REQUEST:" + authorizationString); HttpClient mClient = new HttpClient(); mClient.BaseAddress = new Uri("https://www.googleapis.com/"); mClient.DefaultRequestHeaders.Accept.Clear(); mClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); StringBuilder tokenRequestStringBuilder = new StringBuilder(); tokenRequestStringBuilder.Append("code=" + authorizationString + "&"); tokenRequestStringBuilder.Append("client_id=" + APP_ID + "&"); tokenRequestStringBuilder.Append("client_secret=" + CLIENT_SECRET + "&"); tokenRequestStringBuilder.Append("redirect_uri=http://localhost:" + LISTEN_PORT + "&"); tokenRequestStringBuilder.Append("grant_type=authorization_code"); StringContent mHttpContent = new StringContent(tokenRequestStringBuilder.ToString()); mHttpContent.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded"); string httpContentString = await mHttpContent.ReadAsStringAsync(); Debug.WriteLine("HTTP REQUEST:-------------------\n" + httpContentString); HttpResponseMessage response = await mClient.PostAsync("oauth2/v4/token", mHttpContent); //Debug.WriteLine("TOKEN REQUEST RESPONSE: _-----------------\n" + response.Content); string responseJsonString = await response.Content.ReadAsStringAsync(); GoogleOauthResponse responseDataStruct = JsonConvert.DeserializeObject <GoogleOauthResponse>(responseJsonString); return(responseDataStruct); }
void TryToLoadApi() { UnityEngine.Object textFile; textFile = Resources.Load("API/googlePhotosApi"); TextAsset temp = textFile as TextAsset; string[] keys = temp.text.Split('\n'); clientId = keys[0]; redirectUrl = keys[1]; decriptPassword = keys[2]; // Create sha256 hash mySHA256 = SHA256Managed.Create(); key = mySHA256.ComputeHash(Encoding.ASCII.GetBytes(decriptPassword)); // Create secret IV iv = new byte[16] { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }; Debug.Log("<b>GOOGLE PHOTOS</b> - Loaded api keys from file!"); if (PlayerPrefs.HasKey("GoogleOauthResponse")) { string tempString = DecryptString(PlayerPrefs.GetString("GoogleOauthResponse"), key, iv); currentResponse = JsonConvert.DeserializeObject <GoogleOauthResponse>(tempString); OnLogin(); } }
private async Task HandleHttpAuthCode(string responseLine, int lineNumber) { Debug.WriteLine("Parsing... " + responseLine); string[] stringTokens = responseLine.Split(new char[] { ' ' }); switch (lineNumber) { case 0: string codeString = stringTokens[1].Substring(7); GoogleOauthResponse resp = await RequestToken(codeString); //TODO: store token stuff. for now just display it to show that it works. Debug.WriteLine("Received Token: " + resp.access_token); Device.BeginInvokeOnMainThread(() => DisplayAlert("Received Token!", "token: " + resp.access_token + "\nexpires: " + resp.expires_in + "\nrefresh token: " + resp.refresh_token, "Sweet.")); // don't care about user response. no need to await. break; default: // do nothing break; } }
IEnumerator GetRefreshToken() { UnityWebRequest webRequest = UnityWebRequest.Get($"http://teal-fire.com/MapMoves/refresh.php?refresh={currentResponse.refresh_token}"); yield return(webRequest.SendWebRequest()); if (webRequest.isNetworkError || webRequest.isHttpError) { Debug.Log("<b>GOOGLE PHOTOS</b> - Could not refresh access token"); } else { Debug.Log("<b>GOOGLE PHOTOS</b> - Access token refreshed"); GoogleOauthResponse response = JsonConvert.DeserializeObject <GoogleOauthResponse>(webRequest.downloadHandler.text); response.SetEndTime(); PlayerPrefs.SetString("GoogleOauthResponse", EncryptString(JsonConvert.SerializeObject(response), key, iv)); currentResponse = response; SendRequest(); } }
void TryGetTokenJson() { string tempString = GUIUtility.systemCopyBuffer; try { string decrypted = this.DecryptString(tempString, key, iv); Debug.Log(decrypted); GoogleOauthResponse response = JsonConvert.DeserializeObject <GoogleOauthResponse>(decrypted); response.SetEndTime(); PlayerPrefs.SetString("GoogleOauthResponse", EncryptString(JsonConvert.SerializeObject(response), key, iv)); Debug.Log("<b>GOOGLE PHOTOS</b> - Recieved api keys"); currentResponse = response; waitingForToken = false; animator.SetTrigger("Close"); couldNotRecognise.SetActive(false); loggedIn.SetActive(true); OnLogin(); SendRequest(); } catch (Exception ex) { Debug.Log("Invalid JSON!"); couldNotRecognise.SetActive(true); } }
public void Restart() { currentResponse = null; PlayerPrefs.DeleteKey("GoogleOauthResponse"); OnLogout(); }