So I got this code below to be able to get the leads from Zoho CRM via Power Query.
The StartLogin function seems to work, because when setting the OAuth Credential in VS Code, the window for approving the access appears. When clicking on “approve”, this error appears:
Failed to create OAuth credential due to OAuth Error: [DataFormat.Error] We couldn’t parse your query string because it was improperly formatted.
Unfortunately there is no line indicator, where this error happens. Does anyone know what the problem could be?
section ZohoCRMConnector;
StartLogin = (resourceUrl, state, display) =>
let
authorizeUrl = "https://accounts.zoho.eu/oauth/v2/auth?" & Uri.BuildQueryString([
client_id = {clientId},
response_type = "code",
access_type = "online",
redirect_uri = "https://localhost",
scope = "ZohoCRM.modules.leads.ALL"
])
in
[
LoginUri = authorizeUrl,
CallbackUri = "https://localhost",
WindowHeight = 720,
WindowWidth = 4200,
Context = if state = null then "" else state
];
FinishLogin = (context, callbackUri, state) =>
let
parts = Uri.Parts(callbackUri)[Query],
code = Record.Field(parts, "code"),
data = Json.FromValue([
client_id = {clientId},
client_secret = {clientSecret},
redirect_uri = "https://localhost",
code = code,
grant_type = "authorization_code"
]),
tokenResponse = Web.Contents("https://accounts.zoho.eu/oauth/v2/token", [
Content = data,
Headers = [#"Content-Type" = "application/x-www-form-urlencoded"]
]),
body = Json.Document(tokenResponse),
__ = Diagnostics.Trace(TraceLevel.Information, tokenResponse, null)
in
[
AccessToken = body[access_token],
RefreshToken = body[refresh_token],
TokenExpiresIn = 3600,
Context = if context = null then "" else context,
ContinuationToken = null
];
// Main function to get Zoho CRM leads
[DataSource.Kind="ZohoCRMConnector", Publish="ZohoCRMConnector.Publish"]
shared ZohoCRMConnector.Contents = (optional query as nullable text) =>
let
token = Extension.CurrentCredential()[AccessToken],
url = "https://www.zohoapis.eu/crm/v2/Leads",
headers = [
#"Authorization" = "Zoho-oauthtoken " & token
],
response = Web.Contents(url, [Headers=headers]),
jsonResponse = Json.Document(response)
in
jsonResponse;
// ZohoCRMConnector data source definition
ZohoCRMConnector = [
TestConnection = (dataSourcePath) => { "ZohoCRMConnector.Contents" },
Authentication = [
OAuth = [
StartLogin = StartLogin,
FinishLogin = FinishLogin,
Refresh = Refresh,
Logout = Logout
]
],
Label = "Zoho CRM Leads"
];
ZohoCRMConnector.Publish = [
Beta = true,
Category = "Other",
ButtonText = { "Zoho CRM Leads", "Zoho CRM Leads" },
LearnMoreUrl = "https://www.zoho.eu/crm/developer/docs/api/v2/",
SourceImage = ZohoCRMConnector.Icons,
SourceTypeImage = ZohoCRMConnector.Icons
];
ZohoCRMConnector.Icons = [
Icon16 = { Extension.Contents("ZohoCRMConnector16.png"), Extension.Contents("ZohoCRMConnector20.png"), Extension.Contents("ZohoCRMConnector24.png"), Extension.Contents("ZohoCRMConnector32.png") },
Icon32 = { Extension.Contents("ZohoCRMConnector32.png"), Extension.Contents("ZohoCRMConnector40.png"), Extension.Contents("ZohoCRMConnector48.png"), Extension.Contents("ZohoCRMConnector64.png") }
];
// Refresh function definition
Refresh = (resourceUrl, refresh_token) =>
let
tokenResponse = Web.Contents("https://accounts.zoho.eu/oauth/v2/token", [
Content = Text.ToBinary(Uri.BuildQueryString([
client_id = {clientId},
client_secret = {clientSecret},
refresh_token = refresh_token,
grant_type = "refresh_token"
])),
Headers = [#"Content-Type" = "application/x-www-form-urlencoded"]
]),
body = Json.Document(tokenResponse)
in
[
AccessToken = body[access_token],
TokenExpiresIn = 3600,
RefreshToken = refresh_token
];
// Logout function definition
Logout = (token) =>
let
revokeResponse = Web.Contents("https://accounts.zoho.eu/oauth/v2/token/revoke", [
Content = Text.ToBinary(Uri.BuildQueryString([
token = token
])),
Headers = [#"Content-Type" = "application/x-www-form-urlencoded"]
])
in
revokeResponse;
I tried to identify the error, but I was not able to.