0

I have an iron-ajax element set up:

<iron-ajax
  id="get-tweets"
  url="http://api.twitter.com/1.1/search/tweets.json?q=nasa&result_type=popular"
  handle-as="json"
  on-response="handleResponse"
  debounce-duration="300">
</iron-ajax>

And I'm trying to pass an authentication header to it for Twitter, but I can't figure out how. Twitter has an example request and I'm trying to figure out how to pass the header to the ajax request:

getTweets() {
  const ajax = this.root.querySelector('#get-tweets');
  ajax.headers = Object.assign(ajax.headers, `what do I put here?`
  });
  ajax.generateRequest();
},

I can't seem to figure out how to do it. Let's say I have the following:

Consumer API keys:
abc (API key)
def (API secret key)
Access token & access token secret:
gh-ijk (Access token)
lmn (Access token secret)
Read and write (Access level)

How do I pass this info to the iron-ajax element so I can make the request?

lte__
  • 7,175
  • 25
  • 74
  • 131
  • Secret keys never belong in client side code. You need server side proxy to protect them and to make the requests to the api – charlietfl Nov 21 '18 at 17:47
  • Yeah I'll do that if I publish the code but I just need it to work real quick. Doesn't change the fact that I need to know the correct format on how to pass it. – lte__ Nov 21 '18 at 19:50
  • 1
    But API's that have secret keys aren't CORS enabled usually to prevent people from exposing their credentials – charlietfl Nov 21 '18 at 19:51
  • Ok you know I'd be fine with any solution on how to ping Twitter with iron-ajax. – lte__ Nov 22 '18 at 13:39
  • 1
    Create a server side proxy you call with iron-ajax and server requests the twitter data and returns it to your ajax request – charlietfl Nov 22 '18 at 13:41
  • Can you show me an example? I don't know how to do that. – lte__ Nov 22 '18 at 14:00
  • Do a web search for ajax proxy and include any scripting language your server runs in the search term. Or use the appropriate SDK library https://developer.twitter.com/en/docs/developer-utilities/twitter-libraries.html – charlietfl Nov 22 '18 at 14:01

0 Answers0