Zoho Invoice API 请求令牌时出现错误 {"error":"invalid_client"}

问题描述 投票:0回答:3

访问 Zoho api 以获取令牌时,出现以下错误:

{“错误”:“invalid_client”}

第1步:我请求Auth Code,并且成功返回了Auth Code。

这是我正在使用的API。 https://accounts.zoho.com/oauth/v2/auth?scope=xxx&client_id=yyyyy&state=zzzz&response_type=code&redirect_uri=pppppp&access_type=offline

第 2 步:令牌请求 使用步骤 1 中获得的授权码,我当时正在对令牌进行发布请求,只是我收到了以下异常。

var authTokenRequestData = new
{
    code= code,
    client_id= _clientId,
    client_secret =_clientSecret,
    redirect_uri = _redirectUri,
    grant_type = "authorization_code"
};


var data = new StringContent(JsonConvert.SerializeObject(authTokenRequestData), Encoding.UTF8, "application/json");
    
var url = "https://accounts.zoho.com/oauth/v2/token";

string result = "";

using (var client = new HttpClient())
    {
        var response = await client.PostAsync(url, data);
        result = await response.Content.ReadAsStringAsync();
    }

这给了我例外

{error:invalid_client}

我已验证我的 client_id 和 client_secret。这是唯一正确的。

这是我注册的基于服务器的应用程序客户端。

非常感谢对此的任何帮助。

zoho
3个回答
10
投票

此问题可能还有一个原因。

{"error":"invalid_client"}

检查您是否使用了正确的域名。 (.in .com 或...)

例如,您使用 .in 而不是 .com,反之亦然

就我而言,当我使用 .in 域但我的域是 .com 时,我收到了相同的错误 {"error":"invalid_client"}

我正在使用这个:

var url = "https://accounts.zoho.in/oauth/v2/token"; 

我按如下方式替换了它,这解决了我的问题。

var url = "https://accounts.zoho.com/oauth/v2/token";  

2
投票

我可以通过更改请求内容类型来解决这个问题

"Content-Type", "application/x-www-form-urlencoded"

完整代码如下:

string _domain = ".in"; /*Ensure you're using the correct .com or .in domain*/
var url = "https://accounts.zoho"+ _domain + "/oauth/v2/token";

string result = "";

using (var client = new HttpClient())
    {
        var data = new Dictionary<string, string>
        {
            { "Content-Type", "application/x-www-form-urlencoded" },
            { "code", code },
            { "client_id", _clientId },
            { "client_secret", _clientSecret },
            { "redirect_uri", _redirectUri },
            { "grant_type", "authorization_code" },
            { "access_type", "offline" }
         };

var response = await client.PostAsync(url, new FormUrlEncodedContent(data));
result = await response.Content.ReadAsStringAsync();
    }

0
投票

@Ashish Tripathi 在我的例子中,我将 API url https://accounts.zoho.com 更新为 https://accounts.zoho.in 它工作正常

© www.soinside.com 2019 - 2024. All rights reserved.