Hi ePirat and andypiper if you’re around today.
I have un-methodized my code that composes the http message I send for the INIT.
Sorry it’s not pretty – I took out everything but the essentials. Anyway, here it is:
HttpParams httpParams = new BasicHttpParams();
HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(httpParams, UTF_8);
HttpProtocolParams.setUserAgent(httpParams, HTTP_CORE_1_1);
HttpProtocolParams.setUseExpectContinue(httpParams, false);
httpParams.setParameter("command", "INIT")
.setParameter("media_type", "image/jpeg")
.setParameter("total_bytes", 6046 )
HttpHost httpHost =new HttpHost(UPLOAD_TWITTER_COM, 443);
DefaultHttpClientConnection httpClientConnection = new DefaultHttpClientConnection();
Socket socket = null;
// initialize the HTTPS connection
SSLContext sslcontext = SSLContext.getInstance("TLS");
sslcontext.init(null, null, null);
SSLSocketFactory ssf = sslcontext.getSocketFactory();
socket = ssf.createSocket();
socket.connect(new InetSocketAddress(httpHost.getHostName(), httpHost.getPort()), 0);
httpClientConnection.bind(socket, httpParams);
// see utilty method below
String authorization_header_string = getAuthorizationHeaderString(
null, twitterConsumerKey, oauthNonce
, oauthSignature, oauthTimestamp, encode(oauthTokenAccess), ONE_POINT_ZERO, null
);
BasicHttpEntityEnclosingRequest httpEntityEnclosingRequest =
new BasicHttpEntityEnclosingRequest( "POST", "https://upload.twitter.com/1.1/media/upload.json" );
httpEntityEnclosingRequest.setParams( httpParams );
httpEntityEnclosingRequest.addHeader("Authorization", authorization_header_string);
HttpContext httpContext = new BasicHttpContext(null);
httpContext.setAttribute(ExecutionContext.HTTP_CONNECTION, httpClientConnection);
httpContext.setAttribute(ExecutionContext.HTTP_TARGET_HOST, httpHost);
HttpProcessor httpProcessor = getHttpProcessor();
HttpRequestExecutor httpRequestExecutor = new HttpRequestExecutor();
httpRequestExecutor.preProcess(httpEntityEnclosingRequest, httpProcessor, httpContext);
HttpResponse httpResponse = httpRequestExecutor.execute(
httpEntityEnclosingRequest, httpClientConnection, httpContext);
httpResponse.setParams(httpParams);
httpRequestExecutor.postProcess(httpResponse, httpProcessor, httpContext);
String responseStatusLine = httpResponse.getStatusLine().toString();
Log.d(Constants.TOURMAKER, TAG + " runTwitterUpdate responseStatusLine = " + responseStatusLine);
String responseBody = EntityUtils.toString( httpResponse.getEntity() );
Log.d( Constants.TOURMAKER, TAG + " runTwitterUpdate responsebody = " + responseBody );
if( !isRunTwitterUpdateResponseOK(responseStatusLine, responseBody ) ) return;
/**
* Compose the so-called authorization header string
* @param callback
* @param consumerKey
* @param nonce
* @param signature
* @param timeStamp
* @param token
* @return
*/
@NonNull
private String getAuthorizationHeaderString( String callback, String consumerKey
, String nonce, String signature, String timeStamp, String token, String version, String status ) {
String cb = callback != null ? OAUTH_CALLBACK + EQUAL + callback + COMMA : EMPTY;
String s = signature != null ? COMMA + OAUTH_SIGNATURE + EQUAL + QUOTE + encode(signature) + QUOTE : EMPTY;
String t = token != null ? COMMA + OAUTH_TOKEN + EQUAL + QUOTE + token + QUOTE : EMPTY;
String v = oauthTokenVerifier != null ? COMMA + OAUTH_VERIFIER + EQUAL + QUOTE + oauthTokenVerifier + QUOTE : EMPTY;
String st = status != null ? COMMA + STATUS + EQUAL + QUOTE + encode( status ) + QUOTE : EMPTY;
String authorization_header_string = OAUTH + BLANK
+ cb
+ OAUTH_CONSUMER_KEY + EQUAL + QUOTE + consumerKey + QUOTE
+ COMMA + OAUTH_SIGNATURE_METHOD + EQUAL + QUOTE + HMAC_SHA1 + QUOTE
+ s
+ COMMA + OAUTH_TIMESTAMP + EQUAL + QUOTE + timeStamp + QUOTE
+ COMMA + OAUTH_NONCE + EQUAL + QUOTE + nonce + QUOTE
+ t
+ v
+ st
+ COMMA + OAUTH_VERSION + EQUAL + QUOTE + version + QUOTE;
Log.d(Constants.TOURMAKER, TAG + " authorization_header_string = " + authorization_header_string);
return authorization_header_string;
}
Starting at line 6 of the above code I do:
httpParams.setParameter(“command”, “INIT”)
.setParameter(“media_type”, “image/jpeg”)
.setParameter(“total_bytes”, 6046 )
That’s the part I’m unsure of; is that how one is supposed to send command=INIT, etc.?
Is media_type=“image/jpeg” the reason I get the “media parameter is missing” error?
I hope someone can help me out here. Thanx. You know, I think I should start a new thread that specifically hilites this code. I suspect this thread is stale now. What do you think?