highland18
Hola,
He buscado mucho sobre esto en los últimos días, pero sigo chocando contra una pared de ladrillos. Básicamente, estoy buscando usar la API de Google My Business dentro de Power BI.
Ahora logro conectarme a la API usando el token de acceso en el uri, pero obviamente esto caduca en 1 hora. El siguiente código que estoy usando es:
let ufnGetList = (pgToken) => let result = Json.Document( Web.Contents("***My google business uri here with access token and page token parameter" & pgToken) ), nextPageToken = try result[nextPageToken] otherwise null, reviews = result[reviews], record = [reviews = reviews, nextPageToken = nextPageToken] in record, resultSet = List.Generate( () => ufnGetList(""), each _[nextPageToken] <> null, each ufnGetList(_[nextPageToken]), each [nextPageToken = _[nextPageToken], reviews = _[reviews]] ), lastPageToken = List.Last(Table.FromRecords(resultSet)[nextPageToken]), lastResultSet = ufnGetList(lastPageToken)[reviews], firstResultSet = List.First(Table.FromRecords(resultSet)[reviews]), combineList = List.Combine({ firstResultSet, lastResultSet}),
Lo anterior obtiene el conjunto de datos y recorrerá las páginas para devolver todos los resultados.
Mi problema, como se mencionó, viene con el token de acceso.
Sé que Google usa Oauth 2 y proporciona un token de actualización, pero ¿alguien puede ayudarme a implementar esto en Power Query?
Probé varios métodos de publicación, pero sigo recibiendo un error de solicitud incorrecta (400) de https://accounts.google.com/o/oauth2/token
Intenté seguir la guía de Twitter que existe para usar una API de Oauth 2, pero el código a continuación falla;
/* This M script gets an bearer token and performs a tweet search from the Twitter REST API https://dev.twitter.com/oauth/application-only Requires establishing a Twitter application in order to obtain a Consumer Key & Consumer Secret https://apps.twitter.com/ IMPORTANT - The Consumer Key and Consumer secret should be treated as passwords and not distributed */ let // Concatenates the Consumer Key & Consumer Secret and converts to base64 authKey = "Basic " & Binary.ToText(Text.ToBinary("***my client id***:***my client secret***"),0), url = " https://accounts.google.com/o/oauth2/token", // Uses the Twitter POST oauth2/token method to obtain a bearer token GetJson = Web.Contents(url, [ Headers = [#"Authorization"=authKey, #"Content-Type"="application/x-www-form-urlencoded;charset=UTF-8"], Content = Text.ToBinary("grant_type=authorization") ] ), FormatAsJson = Json.Document(GetJson), // Gets token from the Json response AccessToken = FormatAsJson[access_token], AccessTokenHeader = "bearer " & AccessToken, // Uses the Twitter GET search/tweets method using the bearer token from the previous POST oauth2/token method GetJsonQuery = Web.Contents("***url to access google my business reviews***", [ Headers = [#"Authorization"=AccessTokenHeader] ] ), FormatAsJsonQuery = Json.Document(GetJsonQuery), in FormatAsJsonQuery
Cualquier ayuda es apreciada.
v-yuezhe-msft
@highland18,
Cambie el segundo bloque de código a lo siguiente:
let app_credentials ="client_id=XXXXXXm&client_secret=XXXXXX", url = app_credentials & "&refresh_token=" & "xxxxx" & "&grant_type=refresh_token", GetJson = Web.Contents("https://accounts.google.com/o/oauth2/token", [ Headers = [#"Content-Type"="application/x-www-form-urlencoded"], Content = Text.ToBinary(url) ] ), AccessToken = Json.Document(GetJson)[access_token], AccessTokenHeader = "bearer " & AccessToken, GetJsonQuery = Web.Contents("***url to access google my business reviews***", [ Headers = [#"Authorization"=AccessTokenHeader] ] ), FormatAsJsonQuery = Json.Document(GetJsonQuery) in FormatAsJsonQuery
Hay un hilo similar para su referencia.
https://community.powerbi.com/t5/Service/Refresh-API-GOOGLE-ANALYTICS/td-p/270807
Saludos,
lidia