C# eBay OAuth 合规性 API 身份验证问题

C# eBay OAuth Compliance API Authentication Problems

经过多次努力,我终于能够将 OAuth Authentication/Refresh 令牌进程关闭。我确信我在这个过程中使用的令牌是好的。但我正在努力与合规性 API 沟通,我认为这可能与我的 headers 身份验证过程有关,而不是专门针对合规性 API,但我不确定。我已经尝试了以下代码的许多不同组合,但均未成功。我尝试将调用作为 GET 和 POST(调用应该是 GET)。我已经使用编码和未编码的访问令牌进行了尝试。在尝试了所有不同的代码组合后,我遇到了授权错误或错误请求错误。您可以从注释掉的代码中看到我尝试过的一些不同的东西。感谢您的帮助。

public static string Complaince_GetViolations(string clientId, string ruName, string clientSecret, string accessToken, ILog log)
{
var clientString = clientId + ":" + clientSecret;
//byte[] clientEncode = Encoding.UTF8.GetBytes(clientString);
//var credentials = "Basic " + System.Convert.ToBase64String(clientEncode);

byte[] clientEncode = Encoding.UTF8.GetBytes(accessToken);
var credentials = "Bearer " + System.Convert.ToBase64String(clientEncode);

var codeEncoded = System.Web.HttpUtility.UrlEncode(accessToken);

HttpWebRequest request = WebRequest.Create("https://api.ebay.com/sell/compliance/v1/listing_violation?compliance_type=PRODUCT_ADOPTION")   
as HttpWebRequest;

request.Method = "GET";
//  request.ContentType = "application/x-www-form-urlencoded";

//request.Headers.Add(HttpRequestHeader.Authorization, credentials);
//request.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + codeEncoded);
request.Headers.Add(HttpRequestHeader.Authorization, credentials);
//request.Headers.Add("Authorization", "Bearer " + codeEncoded);
request.Headers.Add("X-EBAY-C-MARKETPLACE-ID", "EBAY-US");


log.Debug("starting request.GetRequestStream");
string result = "";
var response = (HttpWebResponse)request.GetResponse();
using (var streamReader = new StreamReader(response.GetResponseStream())) //FAILS HERE
{
    result = streamReader.ReadToEnd();
}

//DO MORE STUFF BELOW
return "STUFF";
}

我终于找到了解决这个问题的方法。整个承载字符串的 HTML 编码是问题所在。如果将来有人需要这个,欢迎。 =)

            HttpWebRequest request = WebRequest.Create("https://api.ebay.com/sell/compliance/v1/listing_violation?compliance_type=PRODUCT_ADOPTION")   
        as HttpWebRequest;

        request.Method = "GET";
        request.Headers.Add(HttpRequestHeader.Authorization, System.Web.HttpUtility.HtmlEncode("Bearer " + accessToken));
        request.Headers.Add("X-EBAY-C-MARKETPLACE-ID", "EBAY-US");

        log.Debug("starting request.GetRequestStream");
        string result = null;
        var response = (HttpWebResponse)request.GetResponse();
        using (var streamReader = new StreamReader(response.GetResponseStream()))
        {
            result = streamReader.ReadToEnd();
        }