Forum

Use Power Query to ...
 
Notifications
Clear all

Use Power Query to connect to Xero data

5 Posts
3 Users
0 Reactions
171 Views
(@mlinke)
Posts: 39
Trusted Member
Topic starter
 

Hi

I suspect the answer is no, because I can't find anything through a web search, but is it possible to connect to Xero to pull in transaction data?

Thanks

Mardi

 
Posted : 10/08/2020 6:14 pm
(@catalinb)
Posts: 1937
Member Admin
 

Hi,

you can use their API: https://developer.xero.com/documentation/api/invoices

Based on their docs, you have to build your query, with Get data from web.

 
Posted : 11/08/2020 12:09 am
(@mlinke)
Posts: 39
Trusted Member
Topic starter
 

That's great, thanks! I'll check it out.

 
Posted : 11/08/2020 9:16 pm
Philip Treacy
(@philipt)
Posts: 1630
Member Admin
 

The authorization process for the API is quite complicated and I haven't been able to get it to work in PQ in Excel.

You can use Power BI Service to connect to Xero

https://docs.microsoft.com/en-us/power-bi/connect-data/service-connect-to-xero

but there isn't any native connector made for Power BI Desktop.

Phil

 
Posted : 12/08/2020 12:42 am
(@catalinb)
Posts: 1937
Member Admin
 

Philip Treacy said
The authorization process for the API is quite complicated and I haven't been able to get it to work in PQ in Excel.

You can use Power BI Service to connect to Xero

https://docs.microsoft.com/en-us/power-bi/connect-data/service-connect-to-xero

but there isn't any native connector made for Power BI Desktop.

Phil  

Agree, but it is possible though.

A while ago, I had to connect to and read files from a OneDrive folder, that requires a similar process: first, you need to configure an app to collect user consent to access his account, display the login and consent screen, get an authentication code, send the code to get a token, use the token to read user files and create share links for those files.
Displaying the consent screen was solved with vba, there is no OAuth2 connector yet in excel PQ.

 

If you have already the xero application created, you can adjust the code below to display the consent screen and get the authorization code: (the token can be requested using power query using the authorization code)

Sub AuthenticateOneDrive()
Dim GetNewIE As SHDocVw.InternetExplorer, Counter As Long, start As Double, current As Double, Code As String
'create new IE instance
Set GetNewIE = New SHDocVw.InternetExplorer
start = Time

GetNewIE.Navigate2 "https://login.live.com/oauth20_authorize.srf?client_id=" & Range("AppID") & "&scope=onedrive.readwrite&response_type=code&redirect_uri= https://login.live.com/oauth20_desktop.sr f"
GetNewIE.Visible = True
On Error Resume Next
Do Until InStr(GetNewIE.LocationURL, "?code=") > 0
Application.Wait Now + TimeValue("0:00:01")
current = Time
If Round(current - start, 6) > Round(1 / 1440, 6) Then Exit Do 'wait for 30 sec only
Loop
If InStr(GetNewIE.LocationURL, "?code=") > 0 Then
Code = GetNewIE.LocationURL
Code = Right(Code, Len(Code) - InStr(Code, "?code=") - 5)
ThisWorkbook.Worksheets("Authentication").ListObjects("Code").Range.Cells(2) = Code
ThisWorkbook.Connections("Query - Token").Refresh
Else
ThisWorkbook.Worksheets("Authentication").ListObjects("Code").Range.Cells(2).ClearContents
End If
GetNewIE.Quit
Set GetNewIE = Nothing

End Sub

1.jpg2.jpg

Getting the Token with PQ:

let
Code = Excel.CurrentWorkbook(){[Name="Code"]}[Content]{0}

,
ClientID= Excel.CurrentWorkbook(){[Name="AppID"]}[Content]{0}[Column1],
FolderPath= Excel.CurrentWorkbook(){[Name="FolderPath"]}[Content]{0}[Column1],
GetJson = Json.Document(Web.Contents("https://login.live.com",
[RelativePath="/oauth20_token.srf",
Headers = [#"Content-Type"="application/x-www-form-urlencoded"],
Content = Text.ToBinary("client_id=" & ClientID & "&redirect_uri= https://login.live.com/oauth20_desktop.srf&code =" & Code & "&grant_type=authorization_code")
])),

Token = GetJson[access_token]
in
Token

 
Posted : 12/08/2020 11:51 am
Share: