public IEnumerator FetchAccessToken(string grantType, string value) { string authenticationStr = this.clientId + ":" + this.clientSecret; string urlStr = "https://api.noco.tv/1.1/OAuth2/token.php"; var authenticationBytes = System.Text.Encoding.UTF8.GetBytes(authenticationStr); string authenticationB64 = System.Convert.ToBase64String(authenticationBytes); WWWForm form = new WWWForm(); form.AddField("grant_type", grantType); if (grantType == "authorization_code") { form.AddField("code", value); } else { form.AddField(grantType, value); } Dictionary <string, string> headers = form.headers; headers ["Authorization"] = "Basic " + authenticationB64; byte[] rawData = form.data; WWW request = new WWW(urlStr, rawData, headers); yield return(request); result = request.text; this.oauthAccessToken = JsonUtility.FromJson <AccessTokenDescriptor>(result); }
public IEnumerator LoadArchivedAccessToken() { CachedString cache = new CachedString("NocoAccessToken"); string tokenDescription = cache.GetString(); if (tokenDescription != null) { AccessTokenDescriptor previousTokenDescriptor = JsonUtility.FromJson <AccessTokenDescriptor> (tokenDescription); if (cache.IsValid()) { if (debugAuthentification) { Debug.Log("Access token still valid for " + cache.RemainingValidity() + " seconds"); } this.oauthAccessToken = previousTokenDescriptor; } else if (previousTokenDescriptor != null) { if (debugAuthentification) { Debug.Log("Trying to use refresh token after access token lapsing " + tokenDescription); } NocoOAuthAccessTokenRequest request = new NocoOAuthAccessTokenRequest(this.clientId, this.clientSecret); yield return(request.FetchAccessTokenFromRefreshToken(previousTokenDescriptor.refresh_token)); if (debugAuthentification) { Debug.Log("Refresh token call result:" + request.result); } this.oauthAccessToken = request.oauthAccessToken; if (IsAuthenticated()) { if (debugAuthentification) { Debug.Log("Refresh token succed: new access token found: " + this.oauthAccessToken); } this.oauthAccessToken.Save(); } } } }
public IEnumerator Authenticate(String username, String password) { if (username == null || username == "" || password == null || username == "") { Debug.Log("Missing credentials"); yield return(null); } else { // Authentification if (debugAuthentification) { Debug.Log("Noco Authentification..."); } NocoOAuthAuthentificationRequest authentificationRequest = new NocoOAuthAuthentificationRequest(); yield return(authentificationRequest.Launch(username, password, this.clientId)); if (debugAuthentification) { Debug.Log("Authentifiction call finished"); } this.oauthCode = authentificationRequest.code; if (this.oauthCode != null) { NocoOAuthAccessTokenRequest request = new NocoOAuthAccessTokenRequest(this.clientId, this.clientSecret); yield return(request.FetchAccessTokenFromCode(this.oauthCode)); this.oauthAccessToken = request.oauthAccessToken; if (IsAuthenticated()) { this.oauthAccessToken.Save(); } } else { Debug.Log("Authentifiction failed"); } } }
private void loadCachedOAuthInfo() { this.oauthAccessToken = null; }