Passing data from office JS storage to Queries and Connections - excel

I have an Auth flow in my add-in that returns API token which I want later to use in my Queries and Connections (Power BI/Power Query). How can I safely transfer/read the token from Office JS storage to be usable on Power Query Editor?
Right now I created a custom excel function (formula) that returns this token, but those functions can be used only in Cells. There are two problems with that:
The API token will be stored in a cell and thus preserved together with the spreadsheet which is not OK
The power query might not expect the cell to be updated asynchronously as the JS functions are

Related

Microsoft Graph API Sharepoint Excel delay when using the usedRange endpoint

When using the Microsoft Graph API to query the contents of a worksheet via the usedRange endpoint:
https://learn.microsoft.com/en-us/graph/api/worksheet-usedrange?view=graph-rest-1.0&tabs=http
When I edit a cell on the spreadsheet via the browser app and then call that endpoint, the contents of that cell isn't present until after a small delay, usually around 5s. In the worksheet itself the header says that it has been saved, however that isn't immediately present when called via the endpoint.
Is there a method to guarantee that when calling the Graph API endpoint that I'm getting the absolute latest contents, or is there a way to force a save via GraphAPI endpoints before querying for the contents of a worksheet?
I tried refreshSession but that had the same issue, I expect that as soon as it says content saved on the excel sharepoint browser app, the results will be the same as the content returned by usedRange

connection to SQL database with Office script

is it possible to make a connection between Office 365 to SQL database using Office scripts? something simillar to ADODB.Connection in Excel VBA
You can't connect directly to a database using Office Scripts. You can still get data from a database but the process would need to be a bit different:
Instead of querying a database directly, the data would need to be returned to some API (e.g. as JSON)
Instead of providing a username / password to connect to the database, you'd use an API key.
Once you had access to an API and provided an API key, you could use fetch to get the JSON and work with the data in Office Scripts.
You can read more about using fetch with Office Scripts here
In addition to this type of process, you could also trying using something like PowerAutomate to query the database directly.

3-Legged OAuth2 Authorization code flow for Excel?

This might be a long shot, but I'm using the MYOB Business API which has the same OAuth authorization flow as the LinkedIn API - 3-legged - and am planning on making queries to it through Excel, most likely power query.
The big issue with this is obviously dealing with the sending and receiving of the code=, access_token and refresh_token. Most of the searches I've done relating to using Power Query and OAuth seem to only deal with username and password OAuth and not the authorization flow, shown here https://developer.indeed.com/docs/authorization/3-legged-oauth.
I'm curious if there is Power Query M Language code out there that I haven't come across or if I have to go the route of building an OData Feed (or another method?) on top of the API so that it can be queried in Excel?
Any help or further direction would be appreciated.
I should also note they have a website containing their Postman collection using different languages here; https://accountrightapi.myob.cloud/?version=latest#f2b93dfa-803f-44b9-8a07-756874494227
The short answer for Excel is: No, but Asterisk.
Excel Power Query doesn't support custom connectors (as of 2022-02-07)
To securely use Auth2 flow, it requires a custom connector
There is some built in support, if the connector supports it. ( Like Web or OData)
Here's a summarized version of Chris Web. (He's a Power Query Wizard) Connecting To REST APIs With OAuth2 Authentication In Power Query/Power BI # blog.crossjoin.co.uk
the definitive, Microsoft-endorsed answer to this question is:
If want to connect from Power BI to a REST API that uses OAuth2 authentication then you need to build a custom connector.
You can use OAuth2 credential flow in regular Power Query queries without needing a custom connector.
this is not recommended: it’s not secure and it’s not reliable. In particular, hard-coding usernames/passwords or client ids/client secrets in your M code is a really bad idea
The only exception is that you can connect to some APIs that use AAD authentication using the built-in web or OData connectors
To use a custom connector in the Power BI Service, it requires an on-premises gateway
How to implement OAuth2 flow
Creating a Custom Connector
Using built-in OAuth2 flow using with Azure Active Directory using the Web and OData connectors
I used excel VBA code to handle the MYOB API connection by invoking MS Edge, with Seliumn Basic drivers.
VBA then executes the MYOB API Connector Add-On.
VBA receives the database files I require from MYOB and writes them to a.txt file location on my PC.
VBA then refreshes the data connections for Power Query which runs the M Code I’ve written in advanced editor.
When I close excel, VBA closes all the connections and MYOB add on connector.

Power BI Data Source with Refresh ability

I am trying to setup a data set in Power BI which can be refreshed on a need basis or scheduled to refresh.
I am uploading an excel workbook which has a power query.
the power query is connecting to Replicon Service to get data via service. the query looks as below:
Source = Web.Contents(
"https://na2.replicon.com/services/ClientService1.svc/GetActiveClients",
[
Headers=
[
#"Authorization"="Bearer *ValidToken*",
#"Accept"="application/json",
#"Content-Type"="application/json"
],
Content=Text.ToBinary("{}")
]
)
the request is a POST operation and hence Content field is used in Web.Contents Options argument. Authentication is via Bearer token.
Data source setting is Anonymous Credentials with privacy Level set to None.
This works fine and I am able to retrieve the results and even refresh form within workbook.
Once I upload this to PowerBI and attempt to refresh the newly created Dataset it says:
You cannot refresh yet because you need to provide valid credentials for your data sources in the dataset.
So I go to Manage Data sources. Click Edit Credentials. Select "Anonymous" Authentication method and click on Sign In and it says "Login Failed".
Why is that so? It appears that the Headers are lost when I upload the Excel workbook. How can I accomplish this? Is there any alternate ways of being able to set up a Data Set which can be refreshed - the source being a web service?
this is what i ended up doing finally.
my scenario first:
my requests are POST
Authentication is via bearer token which needed to be passed via Request Header. this was the requirement of the replicon service i am trying to invoke which i couldn't change
the dataset that is created in PowerBI needed to be refreshable.
Since i couldn't get it to work directly from inside PowerBI i introduced an intermediate layer. this would interpret GET requests from PowerBI. process the token from query string. accept the service and operation also as query string parameters. it would then create POST request to the Real Service (replicon services in my case). the service name and operation names were also picked up from request URL. the token was pushed as part of request header
so the request from PowerBI would look as PowerBI needed i.e.
Web.Contents("https://intermediatelayer.com?access_token="*validtoken*"&ServiceName="ClientService"&Operation="GetActiveClients"")
not an ideal solution but works.
Manage Data Sources is validating the Anonymous credentials with a GET request to the URL without the hardcoded headers, like you suggest. It's basically running
Web.Contents("https://na2.replicon.com/services/ClientService1.svc/GetActiveClients")
which fails with "(405): Method Not Allowed", and so Manage Data Sources thinks the credentials are wrong.
Short of making the service reply with a success response for the above M, I don't see any way to set up refresh on this mashup.

Power Query Facebook Access Token

I am trying to query the feed of a Facebook group page using Microsoft Power Query.
This particular query requires an access token. I have tried it out successfully in Facebook's Graph API Explorer, however it seems that Power Query does not include the access token when querying the Facebook Graph API. Thus Power Query is returning the "[DataSource.Error] The returned value has no columns." message. Any ideas on how to include\refresh the access token within Excel?
Power Query absolutely does include the access token when using the Facebook Graph API. However, the set of queries that Facebook allows us to run is considerably smaller than what you can do via the API explorer. This is probably why you don't get any data.
I find that the best way to explore or troubleshoot this kind of issue is to use a tool like Fiddler to see the network traffic as the requests are made.

Resources