0

我正在尝试制作一个使用 EVE 在线 API 的“简单”控制台应用程序,它对某些 API 请求使用 Auth0 验证

Swagger 详细信息https://esi.tech.ccp.is/ui/ Auth0 url https://login.eveonline.com/oauth/authorize

我目前正在尝试系统登录,但是我不断收到错误消息

JsonReaderException:解析值时遇到意外字符:<。路径 '',第 0 行,第 0 位置。

我认为我首先做错了这个过程,那只是试图传递默认的 json {"error":"invalid_request","error_description":"Some parameters are either missing or invalid"}

有人可以帮我指出我所缺少的吗

private static void Main()
        {
            var t = Task.Run(() => LoginAsync());
            t.Wait();
        }

private static async Task LoginAsync()
        {
            const string URL = "https://login.eveonline.com/oauth/authorize?";
            AuthenticationApiClient client = new AuthenticationApiClient(new Uri(URL));

        var cred = new Auth0.AuthenticationApi.Models.AuthenticationRequest
        {
            ClientId = "abcdef",
            Username = "UN",
            Password = "PW",
            Scope = "esi-markets.structure_markets.v1",
            Connection = "Username-Password-Authentication"

        };
        Auth0.AuthenticationApi.Models.AuthenticationResponse response = await client.AuthenticateAsync(cred);            
    }

我正在尝试检索身份验证令牌,以便可以使用它进行进一步的请求。

我是 auth0 的新手,而且是个初学者。

非常感谢,

4

1 回答 1

0

您正在尝试使用不正确的隐式授权获取访问令牌。隐式授权应仅在浏览器(Javascript 客户端)中执行。对于非交互式客户端,您可以实现客户端凭据授予资源所有者密码授予来获取访问令牌。首先,创建一个机器对机器客户端并将该客户端凭据用于令牌。以下代码实现客户端凭据授予。

 class Program
    {
        private static string accessToken;
        private static async Task Main(string[] args)
        {
            await ClientCredentialsFlow();
        }

        protected static async Task ClientCredentialsFlow()
        {
            var body = new Model
            {
                grant_type = "client_credentials",
                client_id = "[client id]",
                client_secret = "[client secret]",
                audience = "[API Identifier]"
            };
            using (var client = new HttpClient())
            {
                var content = JsonConvert.SerializeObject(body);
                var stringContent = new StringContent(content, Encoding.UTF8, "application/json");
                var res = await client.PostAsync("https://[domain].auth0.com/oauth/token", stringContent);
                var responseBody = await res.Content.ReadAsStringAsync();
                var deserilizeBody = JsonConvert.DeserializeObject<AuthResponseModel>(responseBody);
                accessToken = deserilizeBody.access_token;
                Console.WriteLine(accessToken);

            }
        }

        internal class Model
        {

            public string grant_type { get; set; }
            public string client_id { get; set; }
            public string client_secret { get; set; }
            public string audience { get; set; }
        }

        internal class AuthResponseModel
        {
            public string access_token { get; set; }
            public string scopes { get; set; }
            public string expires_in { get; set; }
            public string token_type { get; set; }
        }

    }
}

要实现资源所有者密码授予,请查看以下文档。 https://auth0.com/docs/api-auth/tutorials/password-grant

于 2018-08-16T23:23:12.010 回答