Solo gli utenti che hanno acquistato o noleggiato il prodotto possono lasciare commenti
Yutaka Okamoto  

Using the X_OAuth2Configuration script

Configure TweetHelper settings

utilizing Twitter API v2 OAuth 2.0 with PKCE.

   

instructions (1.00.1122)

Kagen.jp(加減)

 

Some of the images used in this instruction manual may not be up to date.

  This procedure manual is current as of November 2023.

It is subject to change in the future.

---------------------------------------------------------------------------

Introduction

This document describes how to set up TweetHelper for "Twitter API v2 OAuth 2.0 with PKCE".

This section describes how to set up your TweetHelper environment.

 

Sign up for TwitterDeveloper and create a Twitter Developer account.

If you have not yet created an account, please refer to the official procedure to create an account.

   

Frequently asked questions about developer accounts and access

https://developer.twitter.com/en/support/twitter-api/developer-account1

 

Script for MetaTrader configuration

Make "X_OAuth2Configuration" ready to run in MetaTrader.

---------------------------------------------------------------------------

1. Check your developer account settings

Twitter Developers > "Settings" > "User authentication settings"

https://developer.twitter.com/en/portal/dashboard



For the "Type of App" field, select "Web App, Automated App or Bot"


It is recommended that the "Callback URI / Redirect URL" in the App info be set to “http://localhost/

This setting is used in "Step 1: Construct an Authorize URL" in "Twitter API v2 OAuth 2.0 with PKCE"


Twitter Developers > "Keys and tokens" > "OAuth 2.0 Client ID and Client Secret"

Issue the following two values and note the values down somewhere.

OAuth 2.0 Client ID

OAuth 2.0 Client Secret


2. Official "Twitter API v2 OAuth 2.0 with PKCE"

Follow the official Doc "How to connect to endpoints using OAuth 2.0 Authorization Code Flow with PKCE

https://developer.twitter.com/en/docs/authentication/oauth-2-0/user-access-token


Step 0: Working with confidential clients

"OAuth 2.0 Client ID" and "OAuth 2.0 Client Secret" values, concatenate them with a single colon (":"), and then encode the resulting string in Base64. Please write down the obtained values.

 

Here is an example using Windows PowerShell commands

Let ${CLIENT_ID} be the OAuth 2.0 Client ID and ${CLIENT_SECRET} be the OAuth 2.0 Client Secret.

[Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes('${CLIENT_ID}:${CLIENT_SECRET}'))

Example

${CLIENT_ID} ID_001

${CLIENT_SECRET} AAAA

   [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes('ID_001:AAAA'))

  Use your favorite tool other than PowerShell to get the string ('${CLIENT_ID}:${CLIENT_SECRET }') in Base64 encoding and make a note of it.


Execute "X_OAuth2Configuration" script (access token acquisition script)

 

MetaTrader preferences (add to WebRequest URL list)


https://api.twitter.com

---------------------------------------------------------------------------------------


Launch "X_OAuth2Configuration

Execute the script "X_OAuth2Configuration" to enter the input configuration state.

This is because the acquisition of the access token must be done within 30 seconds after executing step 2 of "Procedures for connecting using OAuth2.0".

Client ID : OAuth 2.0 Client ID

Auth Basic base64 : String for authentication with Base64 encoding

Response Code : auth_code to be obtained in Step 2 of "Connection Procedure Using OAuth2.0

Callback URI : “Callback URI / Redirect URL" set in AppInfo

--------------------------------------------------------------------------------

Steps to connect using OAuth 2.0

Step 1: Construct an Authorize URL

Authorize URL Template

https://twitter.com/i/oauth2/authorize?response_type=code&client_id=${CLIENT_ID}&redirect_uri=${Callback URI / Redirect URL}&scope=tweet.read%20users.read%20tweet.write&state=state&code_challenge=challenge&code_challenge_method=plain

${CLIENT_ID}  is OAuth 2.0 Client ID

${Callback URI / Redirect URL}  is "Callback URI / Redirect URL" set in AppInfo

   

Example

${CLIENT_ID} is ID_001

${Callback URI / Redirect URL}  is ”http://localhost/”

https://twitter.com/i/oauth2/authorize?response_type=code&client_id=ID_001&redirect_uri=http://localhost/&scope=tweet.read%20users.read%20tweet.write&state=state&code_challenge=challenge&code_challenge_method=plain

Step 2: GET oauth2/authorize

Have the user authenticate, and upon successful authentication, send a request containing an auth_code parameter in the redirect_uri. 

1. Log in to X with the account you wish to post with in a web browser.

2. Open another tab and paste the Authorize URL.

3. Have the user authenticate.

4. get the auth_code from the URL of the redirected page. "auth_code"  of "&code=auth_code".

Example:

http://localhost/?state=state&code=ABCDEFG

 

Copy "ABCDEFG"


Step 3: POST oauth2/token - Access Token

Step 4: Connect to the APIs

Step 5: POST oauth2/token - refresh token

  Go back to the "X_OAuth2Configuration" that was in the input state, paste "auth_code" in the "Response Code" field, and press the "OK" button.

 

Access Token acquisition must be performed within 30 seconds after Step 2 is executed

  If the data cannot be obtained, perform it again from "Step 2: GET oauth2/authorize".

  "X_OAuth2Configuration" will post a message to the authenticated account when the access token is successfully obtained.

 

[TweetHelper]

OAuth2 Configuration success

HELPER NO.1

 

At the same time, it makes the configuration persistent in case the token expires.

 

TweetHelper will automatically request a re-issue of the token just before or after the token expires based on this configuration.

   

Notice:

To authenticate with multiple X accounts, change the HelperNo to get an Access Token.

 

The default is HelperNo.1.

Trial version is fixed to HelperNo.1.


End of Comment

Yutaka Okamoto  

How to Link Posts with Code

[Image]


In the diagram, there are three charts open on MetaTrader:

 

Chart 1: Two indicators installed

Chart 2: One indicator installed

Chart 3: EA (TweetHelper) installed

 

Indicators on Charts 1 and 2: Send the desired message to TweetHelper

EA (TweetHelper) on Chart 3: Sequentially posts messages sent from the indicators


This is an example where messages are being checked on Twitter on a smartphone.

  Instructions for Indicators on Charts 1 and 2


[Key Points]

  1. Include the header file
  2. Initial processing
  3. Check for the existence of TweetHelper
  4. Check for completion of X (Twitter) authentication
  5. Request to post the message


1.Include the header file

#include <WizTweetBridge.mqh>


2.Initial processing

Execute the initTweetBridge() method only once. In indicators or EAs, it is recommended to execute it in the OnInit() event handler.

The argument is the TweetHelper number (usually Helper No.1). 

initTweetBridge(in_helper_no);

3.Check for the existence of TweetHelper

Before requesting a post, execute the isTweetHelperExist() method to check the existence of TweetHelper.

if (! isTweetHelperExist()) {
   // error handling here
   return;
}


4.Check for completion of X (Twitter) authentication

Execute the isTweetHelperReady() method to check for completion of X (Twitter) authentication.

if (! isTweetHelperReady()) {
   // error handling here
   return;
}


5.Request to post the message

Execute the passTweetHelper() method to request the posting of a message.

passTweetHelper(message);


That's all. TweetHelper will then post the message.



Here's a sample script for using TweetHelper:


[Header File:]  WizTweetBridge.mqh

  • For MT4: Place in \MQL4\Include\
  • For MT5: Place in \MQL5\Include\


[Source Code:] helperTest.mq4, helperTest.mq5

  • For MT4: Create with the extension [mq4] and place in \MQL4\Scripts
  • For MT5: Create with the extension [mq5] and place in \MQL5\Scripts


Preparation

  1. Open MetaEditor and build.
  2. Return to MetaTrader, open the Navigator, right-click on the script, and select "Refresh".
  3. Put TweetHelper in standby mode (click the icon and authenticate).

Usage

     4. Double-click on [helperTest] or drag and drop it onto any chart.


[Code]

#property strict
#property script_show_inputs
#include <WizTweetBridge.mqh>
input HELPER_NO   in_helper_no      = TW_HELPER_NO1;                   //- Helper No
input string      in_tweet_txt00    = "";                              //- only one line
input string      in_tweet_txt01    = "";                              //- line No.1
input string      in_tweet_txt02    = "";                              //- line No.2
input string      in_tweet_txt03    = "";                              //- line No.3
input string      in_tweet_txt04    = "";                              //- line No.4
#define LF                       "\n"
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart() {
//---
   initTweetBridge(in_helper_no);
   if (! isTweetHelperExist()) {
      MessageBox("TweetHelper does not exist", "helperTest", MB_OK);
      return;
   }
   if (! isTweetHelperReady()) {
      MessageBox("TweetHelper not ready", "helperTest", MB_OK);
      return;
   }
   if (in_tweet_txt00 != "") {
      if (MessageBox(in_tweet_txt00, "helperTest", MB_OKCANCEL) == IDOK) {
         passTweetHelper(in_tweet_txt00);
      }
   }
   string message = "";
   if (in_tweet_txt01 != "") message += in_tweet_txt01 + LF;
   if (in_tweet_txt02 != "") message += in_tweet_txt02 + LF;
   if (in_tweet_txt03 != "") message += in_tweet_txt03 + LF;
   if (in_tweet_txt04 != "") message += in_tweet_txt04 + LF;
   if (message != "") {
      if (MessageBox(message, "helperTest", MB_OKCANCEL) == IDOK) {
         passTweetHelper(message);
      }
   }
}
//+------------------------------------------------------------------+


File:
Yutaka Okamoto  

How to Link Posts with Code

[Image]

Overall image

In the diagram, there are three charts open on MetaTrader:

 

Chart 1: Two indicators installed

Chart 2: One indicator installed

Chart 3: EA (TweetHelper) installed

 

Indicators on Charts 1 and 2: Send the desired message to TweetHelper

EA (TweetHelper) on Chart 3: Sequentially posts messages sent from the indicators


This is an example where messages are being checked on Twitter on a smartphone.

  Instructions for Indicators on Charts 1 and 2