How to pull data from Toggl API with Power Query?

送分小仙女□ 提交于 2021-02-11 18:11:00

问题


First timer when it comes to connecting to API. I'm trying to pull data from Toggl using my API token but I can't get credentials working. I tried to replicate the method by Chris Webb (https://blog.crossjoin.co.uk/2014/03/26/working-with-web-services-in-power-query/) but I can't get it working. Here's my M code:

let
    Source = Web.Contents(
 "https://toggl.com/reports/api/v2/details?workspace_id=xxxxx&client=xxxxxx6&billable=yes&user_agent=xxxxxxx",
 [
  Query=[ #"filter"="", #"orderBy"=""],
  ApiKeyName="api-token"
 ])
in
    Source

After that I'm inputting my API Token into Web API method in Access Web content windows but I get an error that credentials could not be authenticated. Here's Toggl API specification: https://github.com/toggl/toggl_api_docs/blob/master/reports.md


回答1:


Web.Contents function receives two parameters: url + options

Inside options, you define the headers and the api_key, and other queryable properties, such as:

let
    baseUrl = "https://toggl.com/",
    // the token part can vary depending on the requisites of the API
    accessToken = "Bearer" & "insert api token here"
    options = [
                Headers = [Authorization = accessToken, #"Content-Type" = 
                  "application/Json"], RelativePath ="reports/api/v2/details", Query = 
                  [workspace_id=xxxxx, client=xxxxxx6 , billable=yes, user_agent=xxxxxxx]
              ]
    Source = Web.Contents(baseUrl, options)
    // since Web.Contents() doesn't parse the binaries it fetches, you must use another 
    // function to see if the data was retreived, based on the datatype of the data
    parsedData = Json.Document(Source)
in
    parsedData

The baseUrl is the smallest url that works and never changes; The RelativePath is the next part of the url before the first "?". The Query record is where you define all the attributes to query as a record.

This is usually the format, but check the documentation of the API you're querying to see if it is similar.



来源:https://stackoverflow.com/questions/59152860/how-to-pull-data-from-toggl-api-with-power-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!