Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webhook extend with ability to receive time-limited access token before POST (Google API, spreadsheets) #5408

Open
ww7 opened this issue Dec 4, 2024 · 1 comment
Labels
feature-request Request for new features to be added

Comments

@ww7
Copy link

ww7 commented Dec 4, 2024

πŸ“‘ I have found these related issues/pull requests

Not found related issues.

🏷️ Feature Request Type

New notification-provider, Change to existing notification-provider

πŸ”– Feature description

I need to keep notifications on Google Spreadsheet.

It can be done via HTTP POST (Webhook). But require a time-limited token (to use as a variable at webhook).

βœ”οΈ Solution

Use Google Scripts middleware (see second comment).

❓ Alternatives

Google API OAuth2 working with limited by 60 minutes ACCESS_TOKEN that can be received with e.g. curl:

curl --request POST \
  --data "client_id=$CLIENT_ID&client_secret=$CLIENT_SECRET&grant_type=refresh_token&refresh_token=$REFRESH_TOKEN" \
  https://oauth2.googleapis.com/token

I obtained CLIENT_ID and CLIENT_SECRET at Google Console Spreadsheet API and REFRESH_TOKEN exchanged at OAuth 2.0 Playground (select Spreadsheets v3 for "Authorize APIs" button.)
CleanShot 2024-12-05 at 00 50 26@2x

With such POST a new line will be added to the spreadsheet by curl:

curl --request POST \
  "https://sheets.googleapis.com/v4/spreadsheets/$SPREADSHEET_ID/values/$SHEET_NAME\!A1:append?valueInputOption=USER_ENTERED" \
  --header "Authorization: Bearer $ACCESS_TOKEN" \
  --header 'Content-Type: application/json' \
  --data '{
    "values": [
      ["column 1", "column 2", "column 3"]
    ]
  }'
@ww7 ww7 added the feature-request Request for new features to be added label Dec 4, 2024
@ww7
Copy link
Author

ww7 commented Dec 4, 2024

If request difficult to achieve with Kuma, it can be solved with Google Scripts as middleware.

function doPost(e) {
  try {
    Logger.log('doPost function called');
    
    if (!e.postData || !e.postData.contents) {
      throw new Error('No POST data received');
    }
    
    Logger.log('POST data received: ' + e.postData.contents);
    
    var params = JSON.parse(e.postData.contents);
    
    var secret = params.secret;
    if (secret !== '<secret>') {
      throw new Error('Unauthorized: Invalid secret key');
    }
    delete params.secret;
    
    var status = params.status || 'No Status';
    var message = params.message || 'No Message';
    
    Logger.log('Status: ' + status + ', Message: ' + message);
    
    var spreadsheet = SpreadsheetApp.openById('<table_id>');
    //var spreadsheet = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/<or_such _ink>/edit');

    var sheet = spreadsheet.getSheets()[0];
    
    // example, not adjusted for message from Kuma 
    sheet.appendRow([
      new Date(),
      status,
      message
    ]);
    
    Logger.log('Data appended to sheet');
    
    var response = { result: 'success' };
    Logger.log('Returning response: ' + JSON.stringify(response));
    
    return ContentService.createTextOutput(JSON.stringify(response))
      .setMimeType(ContentService.MimeType.JSON);
    
  } catch (error) {
    Logger.log('Error occurred: ' + error.toString());
    
    var response = { result: 'error', error: error.toString() };
    Logger.log('Returning error response: ' + JSON.stringify(response));
    
    return ContentService.createTextOutput(JSON.stringify(response))
      .setMimeType(ContentService.MimeType.JSON);
  }
}

// Example for test from Script editor
// function testDoPost() {
//   var e = {
//     postData: {
//       contents: JSON.stringify({
//         status: 'Test Status',
//         message: 'Test Message',
//         secret: '<secret>'
//       }),
//       type: 'application/json'
//     }
//   };

//   var result = doPost(e);
//   Logger.log(result.getContent());
// }

Publish on every save and use resulted link with Webhook notification at Kuma with Custom Body as

{
"secret": "<secret>",
"status": "Online",
"message": "Kuma testing"
}

One thing what I didn't get, with curl test it not returning JSON to terminal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request Request for new features to be added
Projects
None yet
Development

No branches or pull requests

1 participant