PDA

View Full Version : HTTP request POST in QT application



abdul_moiz
20th June 2011, 10:52
Hi,
I want to send the following oauth HTTP POST request using qt netwok apis.

url: https://accounts.google.com/o/oauth2/token

POST /accounts/o8/oauth2/token HTTP/1.1
Host: www.google.com
Content-Type: application/x-www-form-urlencoded

client_id=21302922996.apps.googleusercontent.com&
client_secret=XTHhXh1SlUNgvyWGwDk1EjXB&
code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp6&
redirect_uri=urn:ietf:wg:oauth:2.0:oob&
grant_type=authorization_code


Can anybody help me out how to use qt api classes QNetworkRequest,QNetworkAccessManager,QNetworkRepl y for the above HTTP POST request.


Thanks
Abdul Moiz

helloworld
20th June 2011, 15:11
Have you tried something like this:



QNetworkAccessManager *manager = new QNetworkAccessManager(this);

QUrl url("https://accounts.google.com/o/oauth2/token");
QNetworkRequest request(url);

request.setHeader(QNetworkRequest::ContentTypeHead er, "application/x-www-form-urlencoded");

QUrl params;
params.addQueryItem("client_id", "...");
params.addQueryItem("client_secret", "...");
params.addQueryItem("code", "...");
// etc

QObject::connect(manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(replyFinished(QNetworkReply *)));

manager->post(request, params.encodedQuery());

abdul_moiz
21st June 2011, 16:50
Hi,
I have tried with code you have posted but I could not get response from the server i.e the code is not entering into the function replyFinished(QNetworkReply *)

Can you please help me how to debug and find what actually is the problem.

Thanks
ABDUL MOIZ

ChrisW67
22nd June 2011, 08:41
You could help yourself by showing what you have done. Is replyFinished(QNetworkReply *) declared as a slot? Are any warnings issued at program start?

abdul_moiz
23rd June 2011, 08:21
This is what I am trying:

DownloadManager::DownloadManager()
{
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
QUrl url("https://accounts.google.com/o/oauth2/token");
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHead er, "application/x-www-form-urlencoded");
QUrl params;
params.addQueryItem("client_id", "example.com");
params.addQueryItem("client_secret", "example_secret");
params.addQueryItem("code", "example_code");
params.addQueryItem("redirect_uri", "urn:ietf:wg:oauth:2.0:oob");
params.addQueryItem("grant_type", "authorization_code");

QObject::connect(manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(replyfinished(QNetworkReply *)));

QNetworkReply *reply=manager->post(request, params.encodedQuery());

}


void DownloadManager::replyfinished(QNetworkReply *reply)
{
int i;
QByteArray bytes = reply->readAll(); // bytes
qDebug("reply received");

for(i=0;i<=bytes.size();i++)
qDebug() << bytes.at(i);

}


I am not getting any response from server since reply->readAll(); is not returning anything.

Can you help me out.

If else can u please tell me how to use KQOauth/QOauth to access google contacts api.

Thanks in advance

Regards
Abdul Moiz

ChrisW67
24th June 2011, 04:48
See your other thread... the answer is probably the same. Also, a finished transfer is not necessarily a successful transfer. Have you looked at QNetworkReply::error()?