Authentication goes through on twitter API, but when you hit a tweet endpoint, you get 403 forbidden.

Asked 2 years ago, Updated 2 years ago, 62 views

https://moripro.net/gas-twitter-bot/
In the middle of writing the code to tweet from GAS using this as a reference,
The success screen came out after the authentication, but when you run the tweet test function,

You currently have Essential access which includes access to Twitter API v2 endpoints only
The error
I checked and found that v2 can be used without application, but the endpoints on this site seem to be using v1 which needs to be applied for

https://officeforest.org/wp/2021/05/22/gas_twitter_v2/
Please refer to this site
I just copied the service.fetch argument and wrote it.

 {"title": "Forbidden", "detail": "Forbidden", "type": "about:blank", "status":403}

You will receive a response that

It's a 403 error, so it seems like it's a permission issue. Using OAuth 1.0a
https://developer.twitter.com/en/docs/authentication/guides/v2-authentication-mapping
If you look here
Manage Tweets is checked and includes POST/2/tweets endpoints

I don't know how to find the cause from the error message. I would appreciate it if you could let me know if there are any oversights or mistakes.

Here is the code for the GAS

//Variables for authentication
var apikey='xxx';
var apisecret = 'xxx';

vartwitter=TwitterWebService.getInstance(
  apikey, // API Key
  apisecret // API secret key
);
 
//Collaborate and authenticate apps
function authorize(){
  twitter.authorize();
}
 
// Unauthenticate
function reset() {
  twitter.reset();
}
 
// Callback after authentication
function authCallback (request) {
  return twitter.authCallback(request);
}


// Post Tweet
function postTweet(){
  
  varservice=twitter.getService();
  // varendPointUrl='https://api.twitter.com/1.1/statuses/update.json';
  varendPointUrl="https://api.twitter.com/2/tweets";
  
  var message = {
    // Text message body
    text:'Tweet Test'
  }

  var response=service.fetch(endPointUrl,{
    method: 'post',
    muteHttpExceptions—true,
    contentType: 'application/json',
    payload —JSON.stringify (message)
    // payload: { status: 'Tweet Test'}
  });

  console.log(response);
}

Additional information

If you change the API key secret to a dummy string, it will be 401 Unauthorized instead of 403, so the key/secret seems to be correct

javascript google-apps-script twitter

2022-09-29 22:36

1 Answers

I'm a beginner, so I'm sorry if it's wrong, but
Is it because JSON.parse after response= is missing?
I don't know if it's wrong. Sorry


2022-09-29 22:36

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.