2
votes

I'm developing a chatbot using Dialogflow and I need to get full conversation log from it.

I checked this page and I guessed it is able to achieve it by using Stackdriver Logging api.

I referred below page and I tried, however using this api, it occurs 403 error. https://cloud.google.com/logging/docs/reference/v2/rest/v2/logs/list

Did I use this in a wrong way?

How can I resolve this problem?

This is the error message.

{
  "error": {
    "code": 403,
    "message": "The request is missing a valid API key.",
    "status": "PERMISSION_DENIED"
  }
}

This is my code where calling the api. I used Google Apps Script.

function getLogs() {
  //XXXXXXXX is my project_id
  var output = UrlFetchApp.fetch('https://logging.googleapis.com/v2/projects/XXXXXXXX/logs');
  Logger.log(output)
}
1
In the current stage, in order to achieve your goal, it is required to create Google Apps Script project and Cloud Platform Project, then, it is required to link them. By this, the Stackdriver API can be enabled and use it. As the result value, the method of Method: logs.list you try to use is Lists the logs in projects, organizations, folders, or billing accounts. Only logs that have entries are listed.. Is this result value what you want? Can I ask you about whether when you tested "Try this API" at the link you show, you could retrieve the result you want?Tanaike
Thank you for your advice. I guess my Google Apps script and Google Cloud platform have been linked because the linked could platform project is displayed. I checked below way. Apps script menu > resources > could platform project I'll pase the result of "Try this API" in next comment.SasuraiNoTsuba
HTTP/1.1 200 cache-control: private content-encoding: gzip content-length: 174 content-type: application/json; charset=UTF-8 date: Sat, 15 Feb 2020 06:15:20 GMT server: ESF vary: Origin, X-Origin, Referer { "logNames": [ "projects/XXXXX/logs/cloudaudit.googleapis.com%2Factivity", "projects/XXXXX/logs/cloudaudit.googleapis.com%2Fsystem_event", "projects/XXXXX/logs/cloudfunctions.googleapis.com%2Fcloud-functions", "projects/XXXXX/logs/dialogflow_agent", "projects/XXXXX/logs/script.googleapis.com%2Fconsole_logs" ] }SasuraiNoTsuba
but when uncheck "Google OAuth2.0" the same 403 error displayed.SasuraiNoTsuba
You must send authorization header with bearer token (can be retrieved from ScriptApp class)TheMaster

1 Answers

2
votes

I've resolved this way.

  1. Add my api key to http request.
var options = {headers: {Authorization: "Bearer " + ScriptApp.getOAuthToken()}};
var logs = UrlFetchApp.fetch("https://logging.googleapis.com/v2/projects/XXXXXXXX/logs?key=my_api_key", options)
  1. Add scope to appscript.json.
"oauthScopes": ["https://www.googleapis.com/auth/cloud-platform","https://www.googleapis.com/auth/script.external_request"]

Then I found logs.list methond is not appropriate for my goal so I need to change to v2.entries method.

function getLogs(){
var options = {
    method: "post",
    contentType: "application/json",
    headers: {Authorization: 'Bearer ' + ScriptApp.getOAuthToken()},
    payload: JSON.stringify({
      resourceNames: ['projects/XXXXXXX'],
      filter: "timestamp >= searchdate",
      orderBy: "timestamp desc",
      pageSize: 1,
    }),
  }

  var logs = UrlFetchApp.fetch("https://logging.googleapis.com/v2/entries:list?key=my_api_key", options);
  Logger.log(logs);

}