/// <summary> /// Calls <see cref="Modules.TraktSyncModule.GetLastActivitiesAsync()" /> to check, /// whether the given access token is still valid and was not revoked by the user. /// </summary> /// <param name="accessToken">The access token, which will be checked.</param> /// <returns>True, if the given access token was revoked and / or is not valid anymore, otherwise false.</returns> /// <exception cref="ArgumentException">Thrown, if the given access token is null, empty or contains spaces.</exception> /// <exception cref="TraktException">Thrown, if the request <see cref="Modules.TraktSyncModule.GetLastActivitiesAsync()" /> fails.</exception> public async Task <bool> CheckIfAccessTokenWasRevokedOrIsNotValidAsync(string accessToken) { if (string.IsNullOrEmpty(accessToken) || accessToken.ContainsSpace()) { throw new ArgumentException("access token must not be null, empty or contain any spaces", nameof(accessToken)); } var currentAuthorization = Authorization; Authorization = TraktAuthorization.CreateWith(accessToken); try { await Client.Sync.GetLastActivitiesAsync(); return(false); } catch (TraktAuthorizationException) { return(true); } finally { Authorization = currentAuthorization; } }
/// <summary> /// Revokes the given access token. If, successful, the given access token will be invalid /// and the user has to be re-authenticated, e.g. by calling <see cref="TraktOAuth.GetAuthorizationAsync() "/>. /// <para> /// See <a href="http://docs.trakt.apiary.io/#reference/authentication-oauth/get-token/revoke-an-access_token">"Trakt API Doc - OAuth: Revoke Token"</a> for more information. /// </para> /// <para> /// See also <seealso cref="RevokeAuthorizationAsync()" />, <seealso cref="RevokeAuthorizationAsync(string)" />. /// See also <seealso cref="Authorization" />. /// </para> /// </summary> /// <param name="accessToken">The access token, which will be revoked. See also <seealso cref="TraktAuthorization.AccessToken" />.</param> /// <param name="clientId">The Trakt Client ID, which will be used for the request. See also <seealso cref="ClientId" />.</param> /// <exception cref="TraktAuthorizationException"> /// Thrown, if the current <see cref="TraktClient" /> instance is not authorized and the given access token is null, /// empty or contains spaces. /// </exception> /// <exception cref="TraktAuthenticationException">Thrown, if revoking the given access token fails with unknown error.</exception> /// <exception cref="TraktException">Thrown, if the request fails.</exception> /// <exception cref="ArgumentException"> /// Thrown, if the given access token is null, empty or contains spaces. /// Thrown, if the given client id is null, empty or contains spaces. /// </exception> public async Task RevokeAuthorizationAsync(string accessToken, string clientId) { if (!IsAuthorized && (string.IsNullOrEmpty(accessToken) || accessToken.ContainsSpace())) { throw new TraktAuthorizationException("not authorized"); } if (string.IsNullOrEmpty(accessToken) || accessToken.ContainsSpace()) { throw new ArgumentException("access token not valid", nameof(accessToken)); } if (string.IsNullOrEmpty(clientId) || clientId.ContainsSpace()) { throw new ArgumentException("client id not valid", nameof(clientId)); } var postContent = $"token={accessToken}"; var httpClient = TraktConfiguration.HTTP_CLIENT; if (httpClient == null) { httpClient = new HttpClient(); } SetDefaultRequestHeaders(httpClient); SetAuthorizationRequestHeaders(httpClient, accessToken, clientId); var tokenUrl = $"{Client.Configuration.BaseUrl}{TraktConstants.OAuthRevokeUri}"; var content = new StringContent(postContent, Encoding.UTF8, "application/x-www-form-urlencoded"); var response = await httpClient.PostAsync(tokenUrl, content).ConfigureAwait(false); if (!response.IsSuccessStatusCode) { if (response.StatusCode == HttpStatusCode.Unauthorized) { var responseContent = response.Content != null ? await response.Content.ReadAsStringAsync() : string.Empty; throw new TraktAuthenticationException("error on revoking access token") { RequestUrl = tokenUrl, RequestBody = postContent, Response = responseContent, ServerReasonPhrase = response.ReasonPhrase }; } await ErrorHandling(response, tokenUrl, postContent); } else { Client.Authorization = TraktAuthorization.CreateWith(string.Empty, string.Empty); } }