2

Despite it being relatively simple, my brain can't comprehend new concepts all that easily so here I am.

I am using HTTParty to interact with the Microsoft Azure API. Here's an example of how their GET should look:

GET https://api.datamarket.azure.com/data.ashx/amla/text-analytics/v1/GetSentiment?Text=hello+world

Here's the headers they ask to be included:

Authorization: Basic <creds>
Accept: application/json

Where <creds> = ConvertToBase64(“AccountKey:” + yourActualAccountKey);  

Here's my HTTParty class:

  def self.sentiment(phrase)
    require 'base64'
    key = "MYKEYWASHERE"
    accountKey = Base64.encode64("AccountKey:" + key)
    response = get("https://api.datamarket.azure.com/data.ashx/amla/text-analytics/v1/GetSentiment?Text=hello+world", :headers => { "Authorization" => "Basic " + accountKey, "Accept" => "application/json"})
    if response
        return response
    end
  end

It responds with:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd"> <HTML><HEAD><TITLE>Bad Request</TITLE> <META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD> <BODY><h2>Bad Request - Invalid Header</h2> <hr><p>HTTP Error 400. The request has an invalid header name.</p> </BODY></HTML>

I also debugged it and it printed out the following:

opening connection to api.datamarket.azure.com:443...
opened
starting SSL for api.datamarket.azure.com:443...
SSL established
<- "GET /data.ashx/amla/text-analytics/v1/GetSentiment?Text=hello+world HTTP/1.1\r\nAuthorization: Basic QWNj(...)VREUmk1aWdudXRtL2VN\nak(...)R\r\nAccept: application/json\r\nConnection: close\r\nHost: api.datamarket.azure.com\r\n\r\n"

Anything obvious that I am doing wrong here?

regilero
  • 29,806
  • 6
  • 60
  • 99

1 Answers1

0

You have a strange \n character inside of the base64 encoded basic Authorization header. I really don't know how a Base64.encode64() can generate an LF character, you should investigate that. This breaks the HTTp query (you have a new header starting at ak(...) just after the LF (\n) character.

But be careful also not to print debug informations in stack overflow questions with the real base64 encoded key, this encoding is just an encoding, the contant is in clear text in you re-encode it to any charset, so your API key is made public. If you can re-ask for an API key.

Edit the ruby base64 encoding \n error is already known on stack overflo, see this answer for details on the \n added after 60 characters.

Community
  • 1
  • 1
regilero
  • 29,806
  • 6
  • 60
  • 99