Forum Discussion
Get OAuth Custom Connector to check for valid token before initiating login flow again
Yeah unfortunately still getting the same error in spite of trying the steps you outlined.
Here's the updated code, it's the same logic really, but I figured I'd change it to be more like the DataWorld example you shared just in case that was the issue:
[DataSource.Kind="NetsuiteConnector", Publish= "NetsuiteConnector.Publish"]
shared NetsuiteConnector.Contents = (url as text, query as text) =>
let
credential = Extension.CurrentCredential(),
token = if (credential[AuthenticationKind] = "Key") then credential[Key] else credential[access_token],
headers = [ Authorization = "Bearer " & token,#"Prefer" = "transient"],
body = Json.FromValue([q = query]),
source = Json.Document(Web.Contents(url,[Headers = headers, Content = body]))
in
credential;
And I tried clearing the credentials, setting them again and then running the test connection but it still returns:
Hi pbiftw92
My 4th edit of this post 😊: please confirm the OAuth Flow that you have to use and share the link to the documentation:
Is this the relevant link?
https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_158074210415.html
I think we're mixing up the Flows and therefore the auth doesn't work and therefore "The name 'Extension.CurrentCredential' wasn't recognized".
Usually when you see client_id and client_secret, that's usually the Client Credentials Flow which is DIFFERENT than Authorization Code Grant flow that you seem to be using
- pbiftw923 years agoFrequent Visitor
No I'm using authorization code flow: https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_158074210415.html
I think I tried client credentials but couldn't get it to work.
- ams13 years agoResponsive Resident
Thanks pbiftw92
First of all you should make sure that the flow you're using is enabled in Netsuit admin.
Second, IF you're using Authorization Code Grant Flow, then from the documentation (https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_158081952044.html) we see that the 2nd step POST request has to have the following x-www-form-urlencoded parameters:
- code
- redirect_uri
- grant_type = authorization_code
code_verifier(i think this is not needed)
BUT from your code (also below), I see you're passing:
- code
- redirect_uri
- grant_type = authorization_code
- client_id?
- client_secret?
TokenMethod = (grantType, tokenField, code) => let queryString = [ grant_type = "authorization_code", redirect_uri = redirect_uri, client_id = client_id, client_secret = client_secret ], queryWithCode = Record.AddField(queryString, tokenField, code), tokenResponse = Web.Contents(token_uri, [ Content = Text.ToBinary(Uri.BuildQueryString(queryWithCode)), Headers = [ #"Content-type" = "application/x-www-form-urlencoded", #"Accept" = "application/json" ], ManualStatusHandling = {400} ]), body = Json.Document(tokenResponse), result = if (Record.HasFields(body, {"error", "error_description"})) then error Error.Record(body[error], body[error_description], body) else body in result;Sooo, as I see in the documentation, you should remove client_id and client_secret from the queryString and instead place them in the Headers, in the "Authorization" key clientid:clientsecret Base64 encoded -> quote "The client authentication method used in the header of the request follows the HTTP Basic authentication scheme. For more information, see RFC 7617. The format is clientid:clientsecret. The string value is Base64 encoded. "
They provide an example request:
POST /services/rest/auth/oauth2/v1/token HTTP/1.1 Host: <accountID>.suitetalk.api.netsuite Authorization: Basic base64blabla (!!! YOU ARE MISSING THIS !!!) Content-Type: application/x-www-form-urlencoded code=bla_bla&redirect_uri=bla_bla&grant_type=authorization_code&code_verifier=?!Something like:
TokenMethod = (grantType, tokenField, code) => let queryString = [ grant_type = "authorization_code", redirect_uri = redirect_uri ], queryWithCode = Record.AddField(queryString, tokenField, code), tokenResponse = Web.Contents(token_uri, [ Content = Text.ToBinary(Uri.BuildQueryString(queryWithCode)), Headers = [ #"Content-type" = "application/x-www-form-urlencoded", #"Accept" = "application/json", "Authorization" = "Basic " & Binary.ToText(Binary.FromText(client_id & ":" & client_secret, TextEncoding.Utf8), BinaryEncoding.Base64) ], ManualStatusHandling = {400} ]), body = Json.Document(tokenResponse), result = if (Record.HasFields(body, {"error", "error_description"})) then error Error.Record(body[error], body[error_description], body) else body in result;Please mark this as answer if it helped.