saimon24 / ng-twitter

AngularJS library for the Twitter REST Api
http://devdactic.com/
53 stars 12 forks source link

Media upload is not supported.! #9

Open shrinath-shenoy opened 8 years ago

shrinath-shenoy commented 8 years ago

Looks like ngTwitter doesnot support media upload. Also the post calls are appended with get parameters which is not correct - https://github.com/saimon24/ng-twitter/blob/f2a5c90135d5412dd3d0ca55e5f1f903f0658fb9/dist/ng-twitter-api.js#L44-L46

saimon24 commented 8 years ago

Post request has worked for me all the time, guess it's a special problem with the media upload. Feel free to make the changes to the body params and submit a pull request, I would be happy to merge it!

shagul298 commented 8 years ago

Can u share your code for media upload post request.....

ghost commented 7 years ago

I'm having the same trouble, i can't upload an image using POST api with media_id on body request.

I checked the code, and what I understood is that even when make a POST call this part of code adds to url request the params (optional data):

if (parameters !== {}) url = url + '?' + $twitterHelpers.transformRequest(parameters);

This solution is ok when url characters does not exceed the maximum numbers of characters on a HTTP call, but when media_id is too long the call fails.

Could anybody can confirm this?

This is the code i'm using but without success:

    var oauthObject = {
      oauth_consumer_key: clientId,
      oauth_nonce: $cordovaOauthUtility.createNonce(32),
      oauth_signature_method: "HMAC-SHA1",
      oauth_token: token.oauth_token,
      oauth_timestamp: Math.round((new Date()).getTime() / 1000.0),
      oauth_version: "1.0"
    };

    var signatureObj = $cordovaOauthUtility.createSignature("POST", url,
                      oauthObject, {screen_name: token.screen_name}, clientSecret,
                      token.oauth_token_secret);

    $http.defaults.headers.common.Authorization = signatureObj.authorization_header;

    console.log(JSON.stringify(signatureObj));

    var _headers = {
      'Authorization': signatureObj.authorization_header,
      'Content-Type': 'application/x-www-form-urlencoded'
    };

    $http({
      method: "post",
      url: "https://upload.twitter.com/1.1/media/upload.json",
      data: JSON.stringify(base64String)
    }).success(function(data, status, headers, config) {
        console.log(data);
        deferred.resolve(data);
      })
      .error(function(data, status, headers, config) {
        console.log(data);
        deferred.reject(status);
      });

I have tested using both endpoint:


This is the result i got:

Request URL:https://upload.twitter.com/1.1/media/upload.json Request Method:POST Status Code:401 Authorization Required Request Headers POST https://upload.twitter.com/1.1/media/upload.json HTTP/1.1 Accept: application/json, text/plain, / Origin: file:// User-Agent: Mozilla/5.0 (Linux; Android 4.4.2; LG-V500 Build/KOT49I.V50020f) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/30.0.0.0 Safari/537.36 Authorization: OAuth oauth_consumer_key="TG*",oauth_nonce="yyBpmX9yienaHw0xe0izomB3Fp4zCQA0",oauth_signature_method="HMAC-SHA1",oauth_token="191362****",oauth_timestamp="1503497329",oauth_version="1.0",oauth_signature="M8cv3Att*" Content-Type: application/json;charset=UTF-8 Request Payloadview source {,…} media_data: "iv0 + long base64 string" Response Headers content-encoding:gzip content-length:89 content-type:application/json; charset=utf-8 date:Wed, 23 Aug 2017 14:08:47 GMT server:tsa_f strict-transport-security:max-age=631138519 vary:Origin x-connection-hash:c8db7d4f36b7340e16d86e5930b43a6a x-content-type-options:nosniff x-frame-options:SAMEORIGIN x-response-time:126 x-tsa-request-body-time:1 x-xss-protection:1; mode=block

Thank you in advance.