Basic authentication with Qt (QNetworkAccessManager) - qt

I was trying to perform basic authentication for Twitter from my Qt app. I use QNetworkAccessManager. But I couldn't find any help on this.
But I found a program called qsoapmanager which passes credentials in base64 through the header. Maybe I can do this with QNAM by setting header in QNetowrkRequest. But I failed to find a way.
In qsoapman source, header is set like this:
QHttpRequestHeader header;
header.setValue( "Authorization", QString( "Basic " ).append( auth.data() ) );
Can I do just that with QNAM/QNReq or is there a better way?

But if you want to do it by just setting the header value, here's how you can do that:
// HTTP Basic authentication header value: base64(username:password)
QString concatenated = username + ":" + password;
QByteArray data = concatenated.toLocal8Bit().toBase64();
QString headerData = "Basic " + data;
request.setRawHeader("Authorization", headerData.toLocal8Bit());

The recommended way is to connect to the authenticationRequired signal and set the credentials from there.

Just using qNetworkAccessManager normally but add
setRawHeader("Authorization", headerData.toLocal8Bit());
to your request.
Example:
//authentication
QString concatenated = "admin:admin"; //username:password
QByteArray data = concatenated.toLocal8Bit().toBase64();
QString headerData = "Basic " + data;
QNetworkRequest request=QNetworkRequest(QUrl("http://192.168.1.10/getinfo"));
request.setRawHeader("Authorization", headerData.toLocal8Bit());
networkAccessManager->get(request);
`

Related

Formulating POST request to CoinSpot API

I am pulling my hair out trying to query the CoinSpot API.
The endpoint for the Read Only API is: https://www.coinspot.com.au/api/ro
The documentation states:
All requests to the API will need to include the following security
data.
Headers: key - Your API key generated from the settings page sign -
The POST data is to be signed using your secret key according to
HMAC-SHA512 method. Post Params: nonce - Any integer value which must
always be greater than the previous requests nonce value.
I try to query the 'List My Balances' endpoint via: https://www.coinspot.com.au/api/ro/my/balances
However, the code I have formulated below always returns an error: "invalid/missing nonce".
I have tried so many different variations and approaches but it is always the same error.
require(httr)
key <- "68z...39k"
secret <- "71A...48i"
result <- POST("https://www.coinspot.com.au/api/ro/my/balances",
body = list('nonce'=as.integer(as.POSIXct(Sys.time()))), add_headers("key"=key,"sign"=openssl::sha512("https://www.coinspot.com.au/api/ro/my/balances",key = secret)))
content(result)
Any help much appreciated.
Ive struggled with this too- the coinspot API guide isn't very clear.
I figured out you are meant to encode the postdata in correct json format using sha512 and add that to the sign header. See example below querying account balances on v2 of the api.
require(httr)
api_key = "68z...39k"
api_secret = "71A...48i"
base_url = "https://www.coinspot.com.au/api/v2/ro"
request = "/my/balances"
nonce = as.character(as.integer(Sys.time()))
postdata = paste0('{"nonce":"',nonce,'"}') # important to get the quotes correct
api_sign = digest::hmac(api_secret, postdata, algo="sha512",raw=F)
result = POST(paste0(base_url, request),
body = list("nonce"=nonce),
add_headers(c("key"=api_key,
"sign"=api_sign)),
encode="json"
)
cat(rawToChar(result$content))
You would change what is in postdata based on what you are doing with the API- this is a simple example to build on. If you want to see what postdata should look like prior to encryption in sign, use cat(rawToChar(result$request$options$postfields)) after making a request.
For me, I was missing the JSON string encoded postdata in the body, including the nonce. As soon as I added that, it started working.
Heres my code in c# using Restsharp and Newtonsoft
//put the nonce at the beginning
JObject joBody = JObject.Parse(#"{'nonce': '" + DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString() + "'}");
joBody.Merge(originalBody);
var client = new RestClient(_coinspotSettings.BaseURL);
RestRequest request = new RestRequest(endpoint, Method.POST);
request.AddJsonBody(JsonConvert.SerializeObject(joBody));
request.AddHeader("key", coinspotAccount.APIKey);
request.AddHeader("sign", SignData(coinspotAccount, Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(joBody))).ToLower());
request.AddHeader("Content-Type", "application/json");
private string SignData(CoinspotAccount coinspotAccount, byte[] JSONData)
{
var HMAC = new HMACSHA512(Encoding.UTF8.GetBytes(coinspotAccount.APISecret));
byte[] EncodedBytes = HMAC.ComputeHash(JSONData);
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i <= EncodedBytes.Length - 1; i++)
stringBuilder.Append(EncodedBytes[i].ToString("X2"));
return stringBuilder.ToString();
}

How to read Internet header of and email in outlook

I try to read an email Internet headers that we can view it in email properties in outlook app
I ask if there is an option so I can get this I use this code to read the emails in outlook
Outlook::Application outlook;
if (!outlook.isNull())
{
Outlook::NameSpace session(outlook.Session());
session.Logon();
Outlook::MAPIFolder *folder = session.GetDefaultFolder(Outlook::olFolderInbox);
Outlook::Items* mails = new Outlook::Items(folder->Items());
mails->Sort("ReceivedTime");
int num = mails->Count();
ui->label->setText(QString("I have %1 of messages").arg(QString::number(num)));
// Indexing starts from 1
for (int i = 1; i < num; i++)
{
Outlook::MailItem mail(mails->Item(i));
QString s = mail.Subject(); // do something with subject
QString b = mail.Body(); // do something with body
ui->plainTextEdit->appendPlainText("subject : \n" + s);
ui->plainTextEdit->appendPlainText("Body : " + b);
ui->plainTextEdit->appendPlainText("-----------------------------------------------");
}
}
and I was check the Outlook::MailItem for a function to get this Internet header but I not found so if any one try it before or have any idea to solve this
Thanks in advance
You can access the Internet headers via the PR_TRANSPORT_MESSAGE_HEADERS_W property. That property and other MAPI properties are retrievable via the PropertyAccessor object. Note though that individual x-headers are not accessible via a named MAPI property, they are bundled within the message headers so you'll need to parse every line of text to find any particular header record.

Using QHttpMultiPart with QHttpPart Binary and Form Data

I wrote a method to send a image and some form data to a Django Rest Framework Server. The following method works fine, but I would like to know if there is a way to send the form fields together in one QHttpPart, instead of creating one for each field?
Is there a better way to do this? Maybe with less code?
void MyNetwork::sendBinaryFile() {
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyfinished(QNetworkReply*)));
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
QHttpPart aluno, palavra_chave, latitude, latitude_ref, longitude, longitude_ref;
aluno.setHeader(QNetworkRequest::ContentDispositionHeader,QVariant("form-data; name=\"aluno\""));
aluno.setBody("some useful string");
palavra_chave.setHeader(QNetworkRequest::ContentDispositionHeader,QVariant("form-data; name=\"palavra_chave\""));
palavra_chave.setBody("some other useful string");
latitude.setHeader(QNetworkRequest::ContentDispositionHeader,QVariant("form-data; name=\"latitude\""));
latitude.setBody("0");
longitude.setHeader(QNetworkRequest::ContentDispositionHeader,QVariant("form-data; name=\"longitude\""));
longitude.setBody("0");
latitude_ref.setHeader(QNetworkRequest::ContentDispositionHeader,QVariant("form-data; name=\"latitude_ref\""));
latitude_ref.setBody("N");
longitude_ref.setHeader(QNetworkRequest::ContentDispositionHeader,QVariant("form-data; name=\"longitude_ref\""));
longitude_ref.setBody("E");
multiPart->append(aluno);
multiPart->append(palavra_chave);
multiPart->append(latitude);
multiPart->append(latitude_ref);
multiPart->append(longitude);
multiPart->append(longitude_ref);
QHttpPart imagePart;
imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/png"));
imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; filename=\"image.png\"; name=\"file\""));
QFile *file = new QFile("/path/image.png");
file->open(QIODevice::ReadOnly);
imagePart.setBodyDevice(file);
file->setParent(multiPart);
multiPart->append(imagePart);
QString sendPath = "http://localhost/api/images/";
QUrl url(sendPath);
QNetworkRequest request(url);
QString concatenated = "username:password";
QByteArray data = "Basic ";
data.append(concatenated.toLocal8Bit().toBase64());
request.setRawHeader("Authorization", data);
manager->post(request,multiPart);
}
void MyNetwork::replyfinished(QNetworkReply* reply)
{
qDebug() << "C++ - replyUpdateJsonFinished";
qDebug() << QString(reply->readAll());
qDebug() << QString(reply->errorString());
}
Because of the nature of multipart/form-data, each field that is sent must be sent in a different part. This is because each part in the request represents a different field, and is identified by the field name and contains the body that should be sent (in your case, mostly text).
You may have better luck creating a helper method for the text field that takes the field name and the content of it. That method would then be able to create a new part with the correct header (containing the field name) and the body (containing the data to send), without requiring you to repeat the same lines.

C# RichTextBox Displaying Random Format

I have an RS232 communication app with 2 RichTextBox forms. One form is for logging "Outgoing" messages and the other form is for recieving "Incoming" messages from the port. I've used the same syntax for logging messages but for some reason I get random formats in my "Incoming" window (reading from the port).
Is there a way to get the "Incoming" box to display each incoming response byte[] on a new line? I am using a hyper-terminal so anything I send out should come back immediately through the same port.
Both RTB windows use the following log method
private void Log(LogMsgType msgtype, string msgOut)
{
rtbOutgoing.Invoke(new EventHandler(delegate
{
rtbOutgoing.SelectedText = string.Empty;
rtbOutgoing.SelectionFont = new Font(rtbOutgoing.SelectionFont, FontStyle.Bold);
rtbOutgoing.SelectionColor = LogMsgTypeColor[(int)msgtype];
rtbOutgoing.AppendText(msgOut);
rtbOutgoing.ScrollToCaret();
}));
}
Based on your comment, you should be able to try something like this. We're going to change what i.ToString() is outputting so that it's converted to newline delimited data instead of space-delimited:
Try this:
LogOutgoing(LogMsgType.Outgoing, "Short Address" + i.ToString().Replace(" ", "\n") + "\n");
That'll change all spaces into "\n" before the string is sent to the log() function.

Qt5 posting data to server using QUrl / QNetworkRequest

I have a piece of code that worked in 4.8 but now I need to port it to Qt5 (beta2)
This is what should happen:
I want to post some data to a webserver the url should look like this "http://server/actions.php"
Then my fields (a "Action" -string and a "data" string(json)) should be sent to the server using post. Not encoded in the url
QUrl params;
// The data to post
QVariantMap map;
map["Title"]="The title";
map["ProjectId"]="0";
map["Parent"]="0";
map["Location"]="North pole";
map["Creator"]="You";
map["Group"]="a group";
QByteArray data = Json::serialize(map); //the map is converted to json im a QByteArray
params.addEncodedQueryItem("Data",data);
params.addQueryItem("Action", "Update");
QNetworkRequest Request(QUrl("http://server.com/actions.php"));
Request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
NetManager->post(Request,params.encodedQuery());
Now, I might not be doing this right in the first place,(It worked in 4.8) but the real problem is that addEncodedQueryItem() and addQueryItem() are now gone since Qt5 and I don't know what I should replace them with.
I have read the new docs and see the new QUrlQuery but I could not figure out on my own how to use this in my case.
I faced similar problem and used code similiar to the following in Qt5
QUrl url;
QByteArray postData;
url.setUrl("http://myurl....");
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");
Qstring postKey = 'city';
QString postValue = 'Brisbane';
postData.append(postKey).append("=").append(postValue).append("&");
networkManager.post(request,postData);
Hope it might be useful to rewrite your code to send http post values using Qt5
Qt5 no longer has the QUrl::encodedQuery() method. Not sure, but from the documentation it might work using QUrl::query() method instead.
Hope it helps.
QUrlQuery() helps you encode POST data.
Example in PyQt 5.4:
params = QtCore.QUrlQuery()
params.addQueryItem("username", "Вагиф Plaît")
reply = QtNetwork.QNetworkAccessManager().post(request, params.toString(QtCore.QUrl().FullyEncoded))

Resources