示例#1
0
        /// <summary>
        /// Regists the app to Mastodon Instance
        /// </summary>
        /// <returns>RegisteredApp that contains some tokens</returns>
        /// <param name="host">Mastodon host URL</param>
        /// <param name="clientName">Client name what you want</param>
        /// <param name="scope">Scope.</param>
        /// <param name="redirectUri">Redirect URI.</param>
        /// <param name="website">Website that your application</param>
        /// <param name="subdomain">If Auth request do in Instance's subdomain, set this param</param>
        public static async Task <RegisteredApp> RegistApp(string host, string clientName, Scope scope, string redirectUri = null, string website = null, string subdomain = null)
        {
            var param = new Dictionary <string, string>
            {
                { "client_name", clientName },
                { "scopes", scope.ToString(encoding: false) },
                { "redirect_uris", string.IsNullOrEmpty(redirectUri)
                                       ? NoRedirect
                                           : redirectUri }
            };

            if (!string.IsNullOrEmpty(website))
            {
                param.Add("website", website);
            }
            var content = new FormUrlEncodedContent(param);

            var appsSubdomain = string.IsNullOrEmpty(subdomain) ? "" : subdomain + ".";
            var url           = $"https://{appsSubdomain}{host}{ApiMethods.RegistApp}";

            var client   = new HttpClient();
            var response = await client.PostAsync(url, content).ConfigureAwait(false);

            var responseBody = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

            var registerdApp = MastodonJsonConverter.TryDeserialize <RegisteredApp>(responseBody);

            registerdApp.Host  = host;
            registerdApp.Scope = scope;

            return(registerdApp);
        }
示例#2
0
        /// <summary>
        /// Gets the access token by email.
        /// </summary>
        /// <returns>TokenInfo that contains AccessToken</returns>
        /// <param name="host">Host.</param>
        /// <param name="clientId">ClientID</param>
        /// <param name="clientSecret">ClientSecret</param>
        /// <param name="scope">Scope.</param>
        /// <param name="email">Email.</param>
        /// <param name="password">Password.</param>
        /// <param name="subdomain">Subdomain.</param>
        public static async Task <TokenInfo> GetAccessTokenByEmail(string host, string clientId, string clientSecret, Scope scope, string email, string password, string subdomain = null)
        {
            var param = new Dictionary <string, string>
            {
                { "client_id", clientId },
                { "client_secret", clientSecret },
                { "grant_type", "password" },
                { "username", email },
                { "password", password },
                { "scope", scope.ToString(encoding: false) }
            };
            var content = new FormUrlEncodedContent(param);

            var oauthSubdomain = string.IsNullOrEmpty(subdomain) ? "" : subdomain + ".";
            var url            = $"https://{oauthSubdomain}{host}{ApiMethods.OAuthToken}";

            var client   = new HttpClient();
            var response = await client.PostAsync(url, content).ConfigureAwait(false);

            var responseBody = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

            return(MastodonJsonConverter.TryDeserialize <TokenInfo>(responseBody));
        }
示例#3
0
        /// <summary>
        /// Gets the access token by code.
        /// </summary>
        /// <returns>TokenInfo that contains AccessToken</returns>
        /// <param name="host">Host.</param>
        /// <param name="clientId">ClientID</param>
        /// <param name="clientSecret">ClientSecret</param>
        /// <param name="code">Code that got after Auth</param>
        /// <param name="redirectUri">Redirect URI.</param>
        /// <param name="subdomain">Subdomain.</param>
        public static async Task <TokenInfo> GetAccessTokenByCode(string host, string clientId, string clientSecret, string code, string redirectUri = null, string subdomain = null)
        {
            var param = new Dictionary <string, string>
            {
                { "client_id", clientId },
                { "client_secret", clientSecret },
                { "grant_type", "authorization_code" },
                { "code", code.Trim() },
                { "redirect_uri", string.IsNullOrEmpty(redirectUri)
                                       ? NoRedirect
                                           : redirectUri }
            };
            var content = new FormUrlEncodedContent(param);

            var oauthSubdomain = string.IsNullOrEmpty(subdomain) ? "" : subdomain + ".";
            var url            = $"https://{oauthSubdomain}{host}{ApiMethods.OAuthToken}";

            var client   = new HttpClient();
            var response = await client.PostAsync(url, content).ConfigureAwait(false);

            var responseBody = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

            return(MastodonJsonConverter.TryDeserialize <TokenInfo>(responseBody));
        }