Uploading user ids for Tailored Audience / TON

api

#1

I have created everything I need, Campaign, Line Items, Promoted Tweet and the final piece is to get my list of harvested user ids and upload them to my Tailored Audience endpoint which at the minute is too small/empty. All the examples I’ve saw use the ruby script/twurl.

I tried this:

But no joy are there any C# examples of this (not ruby/twurl)?


#2

Hi @SocialOpinions! I don’t understand what is the issue you are confronted with. If the number of user ids is too small you will get an error (I think).

The TON API documentation: https://dev.twitter.com/rest/ton


#3

I am having problems trying to upload a hashed csv file to the TON endpoint. It works with TWURL:

e.g.

C:\Ruby22\bin>twurl -t -H ton.twitter.com /1.1/ton/bucket/ta_partner -X POST -A
"X-TON-Expires: Sat, 10 Nov 2015 23:01:50 GMT" -A “Content-Type: text/csv” -A
Content-Length: 0" --data “100000075”**
opening connection to ton.twitter.com:443
opened
starting SSL for ton.twitter.com:443
SSL established
<- “POST /1.1/ton/bucket/ta_partner HTTP/1.1\r\nX-Ton-Expires: Sat, 10 Nov 2015
23:01:50 GMT\r\nContent-Type: text/csv\r\nContent-Length: 9\r\nAccept-Encoding:
gzip;q=1.0,deflate;q=0.6,identity;q=0.3\r\nAccept: /\r\nUser-Agent: OAuth gem
v0.5.1\r\nAuthorization: OAuth oauth_body_hash=“kNORTxFi3uo2MAdlIWiMxdRlRKY%3D
”, oauth_consumer_key=“XXXXXXXXXXXXXXX”, oauth_nonce=“PEYnSAIKjxxfH
KX3IXXXXXXXXXXXXXRVDXgMILbQ”, oauth_signature=“hBRYBHI%2FFdo%2FMer5xfNXFdlh
zTw%3D”, oauth_signature_method=“HMAC-SHA1”, oauth_timestamp=“1466785718”,
oauth_token=“9XXXXXXXXXXXXXXXXXYYYYYYYYYY”, oauth_versio
n=“1.0”\r\nConnection: close\r\nHost: ton.twitter.com\r\n\r\n”
<- “100000075”
-> “HTTP/1.1 201 Created\r\n”
-> “connection: close\r\n”
-> “content-length: 0\r\n”
-> “content-type: text/csv\r\n”
-> “date: Fri, 24 Jun 2016 16:28:40 GMT\r\n”
-> “location: /1.1/ton/data/ta_partner/ZZZZZZZZ/dOUl3wOD_sGzqL5.csv\r\n”
-> “server: tsa_b\r\n”
-> “set-cookie: guest_id=v1%3A146678572059484764; Domain=.twitter.com; Path=/; E
xpires=Sun, 24-Jun-2018 16:28:40 UTC\r\n”
-> “strict-transport-security: max-age=631138519\r\n”
-> “x-connection-hash: f4645c3fcb29e4d8b720cfb5417c07ae\r\n”
-> “x-content-type-options: nosniff\r\n”
-> “x-rate-limit-limit: 50\r\n”
-> “x-rate-limit-remaining: 49\r\n”
-> “x-rate-limit-reset: 1466786620\r\n”
-> “x-response-time: 18\r\n”
-> “x-tsa-request-body-time: 2\r\n”
-> "\r\n"
reading 0 bytes…
-> ""
read 0 bytes
Conn close

HOWEVER

When I try to use the TON endpoint and pass in the CSV file, I get the following:

C:\Ruby22\bin>twurl -t -H ton.twitter.com /1.1/ton/bucket/ta_partner -X POST -A
"X-TON-Expires: Sat, 10 Nov 2015 23:01:50 GMT" -A “Content-Type: text/csv” -
** “Content-Length: 0” --file “c:\temp\test1_hashed.csv”**
opening connection to ton.twitter.com:443
opened
starting SSL for ton.twitter.com:443
SSL established
<- “POST /1.1/ton/bucket/ta_partner HTTP/1.1\r\nX-Ton-Expires: Sat, 10 Nov 2015
23:01:50 GMT\r\nContent-Type: multipart/form-data, boundary=“00Twurl5244367356
5691708lruwT99”\r\nContent-Length: 224\r\nAccept-Encoding: gzip;q=1.0,deflate;
=0.6,identity;q=0.3\r\nAccept: /\r\nUser-Agent: OAuth gem v0.5.1\r\nAuthoriza
ion: OAuth oauth_body_hash=“uFpxTawhQIBY2QFIGh9840O2IEk%3D”, oauth_consumer_k
y=“XXXXXXXXXXXXXXd”, oauth_nonce=“7UGS6V2bxVGW1BrnjM5iHNuOgqH9YNdO
FirLkainuU”, oauth_signature=“ljQONTMIZACy33sl7RcCLU7wlD8%3D”, oauth_signatu
e_method=“HMAC-SHA1”, oauth_timestamp=“1466785963”, oauth_token=“XXXXXXXX
YYYYYYYYYYYYYYYYYY”, oauth_version=“1.0”\r\nConnection
close\r\nHost: ton.twitter.com\r\n\r\n”
<- “–00Twurl524436735655691708lruwT99\r\nContent-Disposition: form-data; name=
“media[]”; filename=“test1_hashed.csv”\r\nContent-Type: application/octet-st
eam\r\n\r\njamie_maguire1\nsocialopinions\r\n–00Twurl524436735655691708lruwT99
-\r\n”
-> “HTTP/1.1 400 Bad Request\r\n”
-> “cache-control: no-cache\r\n”
-> “connection: close\r\n”
-> “content-length: 0\r\n”
-> “date: Fri, 24 Jun 2016 16:32:45 GMT\r\n”
-> “server: tsa_b\r\n”
-> “set-cookie: guest_id=v1%3A146678596524229469; Domain=.twitter.com; Path=/;
xpires=Sun, 24-Jun-2018 16:32:45 UTC\r\n”
-> “strict-transport-security: max-age=631138519\r\n”
-> “x-connection-hash: 5383257478b4195796b5d47a383efc51\r\n”
-> “x-content-type-options: nosniff\r\n”
-> “x-rate-limit-limit: 50\r\n”
-> “x-rate-limit-remaining: 48\r\n”
-> “x-rate-limit-reset: 1466786620\r\n”
-> “x-response-time: 13\r\n”
-> “x-tsa-request-body-time: 8\r\n”
-> "\r\n"
reading 0 bytes…
-> ""
read 0 bytes
Conn close

Can anyone please help?

Kind Regards.


#4

Things with Windows usually doesn’t work… hahahaha just kidding.

I see both URL equals… did you paste correctly the request here?


#5

Hi, thanks for replying. Yes the urls are thr same but if you look at thr content, one specifies -data, the other specifies -file.

Or am I doing it wrong?


#6

C:\Ruby23-x64\lib\ruby\gems\2.3.0\gems\twurl-0.9.3\lib\twurl\oauth_client.rb

I go to the file below, and comment out the multipart_body for ‘file’, and also the hard setting ‘request.content_type’, and run below command, seems like OK…

twurl -t -H ton.twitter.com /1.1/ton/bucket/ta_partner -X POST -A “Content-Type: text/plain” -A “X-TON-Expires: Sat, 26 Nov 2016 23:01:50 GMT” -f Simple.txt

// oauth_client.rb file

def perform_request_from_options(options, &block)
request_class = METHODS.fetch(options.request_method.to_sym)
request = request_class.new(options.path, options.headers)

  if options.upload && options.upload['file'].count > 0
    boundary = "00Twurl" + rand(1000000000000000000).to_s + "lruwT99"
    multipart_body = []
    file_field = options.upload['filefield'] ? options.upload['filefield'] : 'media[]'
    options.data.each {|key, value|
      multipart_body << "--#{boundary}\r\n"
      multipart_body << "Content-Disposition: form-data; name=\"#{key}\"\r\n"
      multipart_body << "\r\n"
      multipart_body << value
      multipart_body << "\r\n"
    }
    options.upload['file'].each {|filename|
      #multipart_body << "--#{boundary}\r\n"
      #multipart_body << "Content-Disposition: form-data; name=\"#{file_field}\"; filename=\"#{File.basename(filename)}\"\r\n"
      #multipart_body << "Content-Type: application/octet-stream\r\n"
      #multipart_body << "Content-Transfer-Encoding: base64\r\n" if options.upload['base64']
      #multipart_body << "\r\n"
      # http://stackoverflow.com/questions/7157207/how-can-i-read-a-file-with-ruby need to read in binary
      file = File.open(filename, "rb")
      if options.upload['base64']
        #enc = Base64.encode64(File.read(filename))
        enc = Base64.encode64(file.read())
        multipart_body << enc
      else
        #multipart_body << File.read(filename)
        multipart_body << file.read()
      end
    }
    #multipart_body << "\r\n--#{boundary}--\r\n"
    request.body = multipart_body.join
    #request.content_type = "multipart/form-data, boundary=\"#{boundary}\""
  elsif request.content_type && options.data