How to build Twitter authorization header for retrieving attachments from DM?Get oauth credentials!How to make Twitter XAuth post request data for access token?How to create signature for twitter in Salesforce and what are the steps for signature creationHow would you format an Auth/OAuth header like this a Python dict?How to use the curl in android Twitter integrationtwitter digits api AuthorizationTwitter chunked video upload authorizationTwitter Streaming api Oauth - 401 Unauthorized errorTwitter Oauth: How to validate and get user details throughTwitter API send direct message

Why did Jon Snow do this immoral act if he is so honorable?

In the 3D Zeldas, is it faster to roll or to simply walk?

Why does Mjolnir fall down in Age of Ultron but not in Endgame?

Why didn't Thanos use the Time Stone to stop the Avengers' plan?

Is it possible to remotely hack the GPS system and disable GPS service worldwide?

Is the Indo-European language family made up?

NIntegrate doesn't evaluate

What is the difference between singing and speaking?

How did NASA Langley end up with the first 737?

Is it rude to call a professor by their last name with no prefix in a non-academic setting?

Does pair production happen even when the photon is around a neutron?

Popcorn is the only acceptable snack to consume while watching a movie

Question in discrete mathematics about group permutations

Why aren't space telescopes put in GEO?

In general, would I need to season a meat when making a sauce?

Why would Ryanair allow me to book this journey through a third party, but not through their own website?

Construct a word ladder

Remove CiviCRM and Drupal links / banner on profile form

Specific alignment within beginalign environment

Can I summon an otherworldly creature with the Gate spell without knowing its true name?

How to let other coworkers know that I don't share my coworker's political views?

Count rotary dial pulses in a phone number (including letters)

My players want to grind XP but we're using milestone advancement

Efficient Algorithm for the boundary of a set of tiles



How to build Twitter authorization header for retrieving attachments from DM?


Get oauth credentials!How to make Twitter XAuth post request data for access token?How to create signature for twitter in Salesforce and what are the steps for signature creationHow would you format an Auth/OAuth header like this a Python dict?How to use the curl in android Twitter integrationtwitter digits api AuthorizationTwitter chunked video upload authorizationTwitter Streaming api Oauth - 401 Unauthorized errorTwitter Oauth: How to validate and get user details throughTwitter API send direct message






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I want be able to open attachments from my inbox via direct url. So I trying to generate Twitter OAuth header, respecting docs https://developer.twitter.com/en/docs/direct-messages/message-attachments/guides/retrieving-media.html, but response is always 401 Unathorized, and I don't know why.



I made all steps from documentation with full accuracy, double-checked. All should works, but it doesn't, and I have no idea why. Every response that I got from Twitter have http code 401. Tokens absolutely correct.



This is not a duplicate: I already checked similar questions, code from it doesn't works as well even if I copy-paste. I also can't use Abraham's Twitteroauth library because it have support only for API and OAuth endpoints, not for direct media links.



Here function that creates authorization header. You can see full code here: https://pastebin.com/yBEVW9KN



<?php

function getAuthorizationHeader(string $url)

$params = array(
"oauth_consumer_key" => $this->consumer_key,
"oauth_nonce" => $this->getNonce(),
"oauth_signature_method" => "HMAC-SHA1",
"oauth_timestamp" => time(),
"oauth_token" => $this->access_token,
"oauth_version" => "1.0",
);

// Step 1. Percent encode.
$_temp = [];
foreach ($params as $key => $value)
$_temp[rawurlencode($key)] = rawurlencode($value);

$params = $_temp;

// Step 2. Sort.
ksort($params);

// Step 3. Build DST
$dst = '';
foreach ($params as $key => $value)
$dst .= $key . '=' . $value . '&';

$dst = rtrim($dst, '&');

// Step 4. Creating signature base string
$baseString = 'GET' . '&' . rawurlencode($url) . '&' . rawurlencode($dst);

// Step 5. Generating signature key
$signingKey = rawurlencode($this->consumer_secret) . '&' . rawurlencode($this->access_token_secret);

// Step 6. Finally, signature.
$signature = base64_encode(hash_hmac('sha1', $baseString, $signingKey, true));

// Step 7. Build header string.
$header[] = 'authorization: ';
$header[] = 'OAuth ';

// Maybe ordering important as well?
$header[] = 'oauth_consumer_key="' . $params['oauth_consumer_key'] . '",';
$header[] = 'oauth_nonce="' . $params['oauth_nonce'] . '",';
$header[] = 'oauth_signature="' . $signature . '",';
$header[] = 'oauth_signature_method="' . $params['oauth_signature_method'] . '",';
$header[] = 'oauth_timestamp="' . $params['oauth_timestamp'] . '",';
$header[] = 'oauth_token="' . $params['oauth_token'] . '",';
$header[] = 'oauth_version="' . $params['oauth_version'] . '"';

return implode('', $header);











share|improve this question




























    0















    I want be able to open attachments from my inbox via direct url. So I trying to generate Twitter OAuth header, respecting docs https://developer.twitter.com/en/docs/direct-messages/message-attachments/guides/retrieving-media.html, but response is always 401 Unathorized, and I don't know why.



    I made all steps from documentation with full accuracy, double-checked. All should works, but it doesn't, and I have no idea why. Every response that I got from Twitter have http code 401. Tokens absolutely correct.



    This is not a duplicate: I already checked similar questions, code from it doesn't works as well even if I copy-paste. I also can't use Abraham's Twitteroauth library because it have support only for API and OAuth endpoints, not for direct media links.



    Here function that creates authorization header. You can see full code here: https://pastebin.com/yBEVW9KN



    <?php

    function getAuthorizationHeader(string $url)

    $params = array(
    "oauth_consumer_key" => $this->consumer_key,
    "oauth_nonce" => $this->getNonce(),
    "oauth_signature_method" => "HMAC-SHA1",
    "oauth_timestamp" => time(),
    "oauth_token" => $this->access_token,
    "oauth_version" => "1.0",
    );

    // Step 1. Percent encode.
    $_temp = [];
    foreach ($params as $key => $value)
    $_temp[rawurlencode($key)] = rawurlencode($value);

    $params = $_temp;

    // Step 2. Sort.
    ksort($params);

    // Step 3. Build DST
    $dst = '';
    foreach ($params as $key => $value)
    $dst .= $key . '=' . $value . '&';

    $dst = rtrim($dst, '&');

    // Step 4. Creating signature base string
    $baseString = 'GET' . '&' . rawurlencode($url) . '&' . rawurlencode($dst);

    // Step 5. Generating signature key
    $signingKey = rawurlencode($this->consumer_secret) . '&' . rawurlencode($this->access_token_secret);

    // Step 6. Finally, signature.
    $signature = base64_encode(hash_hmac('sha1', $baseString, $signingKey, true));

    // Step 7. Build header string.
    $header[] = 'authorization: ';
    $header[] = 'OAuth ';

    // Maybe ordering important as well?
    $header[] = 'oauth_consumer_key="' . $params['oauth_consumer_key'] . '",';
    $header[] = 'oauth_nonce="' . $params['oauth_nonce'] . '",';
    $header[] = 'oauth_signature="' . $signature . '",';
    $header[] = 'oauth_signature_method="' . $params['oauth_signature_method'] . '",';
    $header[] = 'oauth_timestamp="' . $params['oauth_timestamp'] . '",';
    $header[] = 'oauth_token="' . $params['oauth_token'] . '",';
    $header[] = 'oauth_version="' . $params['oauth_version'] . '"';

    return implode('', $header);











    share|improve this question
























      0












      0








      0


      1






      I want be able to open attachments from my inbox via direct url. So I trying to generate Twitter OAuth header, respecting docs https://developer.twitter.com/en/docs/direct-messages/message-attachments/guides/retrieving-media.html, but response is always 401 Unathorized, and I don't know why.



      I made all steps from documentation with full accuracy, double-checked. All should works, but it doesn't, and I have no idea why. Every response that I got from Twitter have http code 401. Tokens absolutely correct.



      This is not a duplicate: I already checked similar questions, code from it doesn't works as well even if I copy-paste. I also can't use Abraham's Twitteroauth library because it have support only for API and OAuth endpoints, not for direct media links.



      Here function that creates authorization header. You can see full code here: https://pastebin.com/yBEVW9KN



      <?php

      function getAuthorizationHeader(string $url)

      $params = array(
      "oauth_consumer_key" => $this->consumer_key,
      "oauth_nonce" => $this->getNonce(),
      "oauth_signature_method" => "HMAC-SHA1",
      "oauth_timestamp" => time(),
      "oauth_token" => $this->access_token,
      "oauth_version" => "1.0",
      );

      // Step 1. Percent encode.
      $_temp = [];
      foreach ($params as $key => $value)
      $_temp[rawurlencode($key)] = rawurlencode($value);

      $params = $_temp;

      // Step 2. Sort.
      ksort($params);

      // Step 3. Build DST
      $dst = '';
      foreach ($params as $key => $value)
      $dst .= $key . '=' . $value . '&';

      $dst = rtrim($dst, '&');

      // Step 4. Creating signature base string
      $baseString = 'GET' . '&' . rawurlencode($url) . '&' . rawurlencode($dst);

      // Step 5. Generating signature key
      $signingKey = rawurlencode($this->consumer_secret) . '&' . rawurlencode($this->access_token_secret);

      // Step 6. Finally, signature.
      $signature = base64_encode(hash_hmac('sha1', $baseString, $signingKey, true));

      // Step 7. Build header string.
      $header[] = 'authorization: ';
      $header[] = 'OAuth ';

      // Maybe ordering important as well?
      $header[] = 'oauth_consumer_key="' . $params['oauth_consumer_key'] . '",';
      $header[] = 'oauth_nonce="' . $params['oauth_nonce'] . '",';
      $header[] = 'oauth_signature="' . $signature . '",';
      $header[] = 'oauth_signature_method="' . $params['oauth_signature_method'] . '",';
      $header[] = 'oauth_timestamp="' . $params['oauth_timestamp'] . '",';
      $header[] = 'oauth_token="' . $params['oauth_token'] . '",';
      $header[] = 'oauth_version="' . $params['oauth_version'] . '"';

      return implode('', $header);











      share|improve this question














      I want be able to open attachments from my inbox via direct url. So I trying to generate Twitter OAuth header, respecting docs https://developer.twitter.com/en/docs/direct-messages/message-attachments/guides/retrieving-media.html, but response is always 401 Unathorized, and I don't know why.



      I made all steps from documentation with full accuracy, double-checked. All should works, but it doesn't, and I have no idea why. Every response that I got from Twitter have http code 401. Tokens absolutely correct.



      This is not a duplicate: I already checked similar questions, code from it doesn't works as well even if I copy-paste. I also can't use Abraham's Twitteroauth library because it have support only for API and OAuth endpoints, not for direct media links.



      Here function that creates authorization header. You can see full code here: https://pastebin.com/yBEVW9KN



      <?php

      function getAuthorizationHeader(string $url)

      $params = array(
      "oauth_consumer_key" => $this->consumer_key,
      "oauth_nonce" => $this->getNonce(),
      "oauth_signature_method" => "HMAC-SHA1",
      "oauth_timestamp" => time(),
      "oauth_token" => $this->access_token,
      "oauth_version" => "1.0",
      );

      // Step 1. Percent encode.
      $_temp = [];
      foreach ($params as $key => $value)
      $_temp[rawurlencode($key)] = rawurlencode($value);

      $params = $_temp;

      // Step 2. Sort.
      ksort($params);

      // Step 3. Build DST
      $dst = '';
      foreach ($params as $key => $value)
      $dst .= $key . '=' . $value . '&';

      $dst = rtrim($dst, '&');

      // Step 4. Creating signature base string
      $baseString = 'GET' . '&' . rawurlencode($url) . '&' . rawurlencode($dst);

      // Step 5. Generating signature key
      $signingKey = rawurlencode($this->consumer_secret) . '&' . rawurlencode($this->access_token_secret);

      // Step 6. Finally, signature.
      $signature = base64_encode(hash_hmac('sha1', $baseString, $signingKey, true));

      // Step 7. Build header string.
      $header[] = 'authorization: ';
      $header[] = 'OAuth ';

      // Maybe ordering important as well?
      $header[] = 'oauth_consumer_key="' . $params['oauth_consumer_key'] . '",';
      $header[] = 'oauth_nonce="' . $params['oauth_nonce'] . '",';
      $header[] = 'oauth_signature="' . $signature . '",';
      $header[] = 'oauth_signature_method="' . $params['oauth_signature_method'] . '",';
      $header[] = 'oauth_timestamp="' . $params['oauth_timestamp'] . '",';
      $header[] = 'oauth_token="' . $params['oauth_token'] . '",';
      $header[] = 'oauth_version="' . $params['oauth_version'] . '"';

      return implode('', $header);








      php twitter twitter-oauth






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 24 at 2:14









      RaymondRaymond

      4016




      4016






















          0






          active

          oldest

          votes












          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55320158%2fhow-to-build-twitter-authorization-header-for-retrieving-attachments-from-dm%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55320158%2fhow-to-build-twitter-authorization-header-for-retrieving-attachments-from-dm%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

          Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript