Twitter username validation does not work

Created on 26 February 2021, almost 4 years ago
Updated 21 March 2023, over 1 year ago

There is an issue with username validation.

The code is:

    if ($validate = $field_settings->getSetting('validate_existance')) {
      try {
        // Query the Twitter User page.
        // Since v1.1, we could not request the API with OAuth token.
        $client = \Drupal::httpClient();
        $client->get('https://twitter.com/' . $twitter_username, ['method' => 'HEAD']);
      }
      catch (RequestException $e) {
        $response_code = $e->getCode();

        // HTTP status code 404 means the username doesn't exist.
        if ($response_code == 404) {
          $form_state->setError($element, t("The twitter username doesn't exist."));
        }
        // Log and display an  error if we get an unexpected status code.
        else {
          $message = "The Twitter API returned the unexpected status code %code. That means it's not guaranteed the username %username actually exists.";
          $message_args = [
            '%code' => $response_code,
            '%username' => $twitter_username,
          ];

          $this->messenger()->addWarning(t("The Twitter API returned the unexpected status code %code. That means it's not guaranteed the username %username actually exists.", $message_args));
          \Drupal::logger('twitter_username')->notice($message, $message_args);
        }
      }
    }

But it doesn't work now. Probably it worked several years ago but now it doesn't.
Twitter returns valid pages for both
existing account – https://twitter.com/drupal
and not existing – https://twitter.com/drupallll

I've tried to find the way to distinguish existing and non-existing account pages but there are no suitable ways.

We can do that with the following ways:
1.) parse html on page – but any change in HTML from twitter could break our logic.
2.) use API calls - I think it's huge overhead for such small piece of functionality.

So I recommend to remove this part from the module at all.
Because even non existing twitter links looks ok.

I have maintainers rights but I'd like to ask @opi to do that change.

πŸ› Bug report
Status

Fixed

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡¦Ukraine nnevill Lutsk

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024