TwitterAPI​の仕様が変わって、T​witter関数では​データにアクセスでき​ません twitte​r.mのどこを変えれ​ばよいでしょうか。

2 views (last 30 days)
Yuji Ochiai
Yuji Ochiai on 19 Mar 2022
Edited: Chetan on 2 Jan 2024
お願いいたします。2022年3月にTwitter APIの申請をしてアカウントができましたが、APIの仕様が変わって、
API Key
API Key Secret
Bearer Token
Access Token
Access Token Secret
の5つのキーが与えられます。SentimentAnalysis.mlxを使ってキーを設定してアクセスを試みましたが、Bearer Token はTwitter関数では定義されていないようで、アクセス拒否になります。Twitter APIは1.1でアカウントは設定しています。pythonなどで、アクセスすることはできますが、必要なキーは、Bearer Token だけでOKでした。twitter.mに何かを書き加えれば、使用できるか、あるいは、別な方法があるか、お教えいただけるとありがたいです。お手数ですが、アドバイスいただけるとありがたいです。

Answers (1)

Chetan
Chetan on 2 Jan 2024
Edited: Chetan on 2 Jan 2024
I understand that you're encountering challenges with the updated Twitter API, particularly with its shift to OAuth 1.0 and the requirement to use a Bearer token for authentication.
Here's how you can adapt to these changes:
1. Custom Implementation for Twitter API:
- You can create a custom implementation to interact with the Twitter API. To pass the Bearer token in a web request, use the following MATLAB code:
headers = {'Authorization', ['Bearer ', yourBearerToken]};
options = weboptions('HeaderFields', headers);
response = webread(yourRequestURL, options);
- This code snippet sets up the necessary HTTP headers with your Bearer token and then performs the web request.
- You can automate the process of capturing the Bearer token by writing a MATLAB script that retrieves it at runtime.
2. Use Twitty Tool:
  • Twitty is a third-party Twitter API client for MATLAB that simplifies the process of interfacing with the Twitter API.
  • To get started with Twitty, follow these steps:
  • Download Twitty from the MATLAB File Exchange or GitHub.
  • Add the Twitty folder to your MATLAB path using the `addpath` function.
  • Create a `credentials.json` file containing your Twitter API keys:
{
"ConsumerKey": "YOUR_CONSUMER_KEY",
"ConsumerSecret": "YOUR_CONSUMER_SECRET",
"AccessToken": "YOUR_ACCESS_TOKEN",
"AccessTokenSecret": "YOUR_ACCESS_TOKEN_SECRET"
}
  • Load your credentials in MATLAB and instantiate a Twitty object:
credentials = loadjson('credentials.json');
twitterClient = twitty(credentials); % Creates a Twitty object
  • With the Twitty object, you can now make calls to the Twitter API using the methods provided by the Twitty class.
Refer to the following MathWorks Documentation for detailed infromation
Hope it helps

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!