How can I access auth-only Twitter API methods from a web application

后端 未结 2 1316
刺人心
刺人心 2021-02-09 14:47

I have a web application for iPhone, which will ultimately run within a PhoneGap application - but for now I\'m running it in Safari.

The application needs to access twe

2条回答
  •  我在风中等你
    2021-02-09 15:28

    I was struggling with similar problem of making JSONP requests from Jquery, the above answer helped just to add what I did to achieve my solution.

    I am doing server to server oauth and then I send oauth token, secret, consumer key and secret (this is temporary solution by the time we put a proxy to protect consumer secret). You can replace this to token acquiring code at client.

    Oauth.js and Sha1.js download link! Once signature is generated.

    Now there are 2 problems:

    1. JSONP header cannot be edited
    2. Signed arguments which needs to be sent as part of oauth have problem with callback=? (a regular way of using JSONP).

    As above answer says 1 cannot be done. Also, callback=? won't work as the parameter list has to be signed and while sending the request to remote server Jquery replace callback=? to some name like callback=Jquery1232453234. So a named handler has to be used.

    function my_twitter_resp_handler(data){
        console.log(JSON.stringify(data));
    }
    

    and getJSON did not work with named function handler, so I used

    var accessor = {
                       consumerSecret: XXXXXXXXXXXXXXXXXXXXXX,
                       tokenSecret   : XXXXXXXXXXXXXXXXXXXXXX
    
                     };
    
      var message = {  action: "https://api.twitter.com/1/statuses/home_timeline.json",
                       method: "GET",
                       parameters: []
                    };
      message.parameters.push(['realm', "https://api.twitter.com/1/statuses/home_timeline.json"]);
      message.parameters.push(['oauth_version', '1.0']);
      message.parameters.push(['oauth_signature_method', 'HMAC-SHA1']);
      message.parameters.push(['oauth_consumer_key', XXXXXXXXXXXXXXXX]);
      message.parameters.push(['oauth_token', XXXXXXXXXXXXXXX]);
      message.parameters.push(['callback', 'my_twitter_resp_handler']);
    
      OAuth.completeRequest(message, accessor);
    
      var parameterMap = OAuth.getParameterMap(message.parameters);
    

    Create url with base url and key value pairs from parameterMap

    jQuery.ajax({ 
                   url: url, 
                   dataType: "jsonp",
                   type: "GET",
                  });
    

提交回复
热议问题