i can't find what wrong in my code. Im try test use this data at https://www.hurl.it and it return json.
But when run Qt, i got error
1: "4/ufy-e-ud4_3XxlHYN7VNp0nZB9G26EzLX_CrEt0F3QA"
***** request url
= QUrl("www.googleapis.com/oauth2/v4/token") ***** request content type header= 0
***** call POST *****
qt.network.ssl: QSslSocket: cannot resolve SSLv2_client_method
qt.network.ssl: QSslSocket: cannot resolve SSLv2_server_method
5
***** finishedSlot! *****
***** reply http error = 301
***** response ***** ""
1: "4/ufy-e-ud4_3XxlHYN7VNp0nZB9G26EzLX_CrEt0F3QA"
***** request url= QUrl("www.googleapis.com/oauth2/v4/token")
***** request content type header= 0
***** call POST *****
qt.network.ssl: QSslSocket: cannot resolve SSLv2_client_method
qt.network.ssl: QSslSocket: cannot resolve SSLv2_server_method
5
***** finishedSlot! *****
***** reply http error = 301
***** response ***** ""
To copy to clipboard, switch view to plain text mode
PLZ ANYONE CAN HELP ?
#include "gettoken.h"
#include <QNetworkRequest>
#include <QFile>
#include <QDir>
#include <QTextCodec>
#include <QUrlQuery>
#include <QUrl>
#include <QSslSocket>
{
appID = "1037983989117-p3cllh8ou68c32v9irrh7i9fcdk8egs7.apps.googleusercontent.com";
secretString = "uQQZK2Jb9XOprgYzYjrrWj3s";
qReply = Q_NULLPTR;
accessUrl = "www.googleapis.com/oauth2/v4/token";
qnam = new QNetworkAccessManager(this);
}
void getToken
::requestToken(QString code
) {
qDebug()<<"1: "<<code; //very sure this is right code
QNetworkRequest qRequest(setUrl);
qRequest.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");
connect(qnam,SIGNAL(finished(QNetworkReply *)), this,SLOT(writeFile(QNetworkReply *)));
qByte.append("code="+code+"&");//very sure this is right code
qByte.append("client_id=1037983989117-p3cllh8ou68c32v9irrh7i9fcdk8egs7.apps.googleusercontent.com&");
qByte.append("client_secret=uQQZK2Jb9XOprgYzYjrrWj3s&");
qByte.append("redirect_uri=http://localhost:8080/cb&");
qByte.append("grant_type=authorization_code");
qDebug() << "***** request url=" << qRequest.url();
qDebug() << "***** request content type header=" << qRequest.ContentTypeHeader;
qDebug() << "***** call POST *****";
qnam->post(qRequest, qByte);
// qnam.post(qRequest,qByte);
qDebug()<<"5";
// qDebug()<<"almost done";
}
void getToken::writeFile(QNetworkReply *mReply)
{
qDebug() << "***** finishedSlot! *****";
// Reading attributes of the reply
// e.g. the HTTP status code
mReply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
// Or the target URL if it was a redirect:
mReply->attribute(QNetworkRequest::RedirectionTargetAttribute);
// see CS001432 on how to handle this
// no error received?
if (mReply->error() == QNetworkReply::NoError)
{
// read data from QNetworkReply here
// Example 2: Reading bytes form the reply
QString stringResponse
(bytes
);
// string
qDebug() << "***** response *****" << stringResponse;
}
// Some http error received
else
{
qDebug() << "***** reply http error =" <<(int) mReply->error();
QString stringResponse
(bytes
);
// string qDebug() << "***** response *****" << stringResponse;
}
// We receive ownership of the reply object
// and therefore need to handle deletion.
delete mReply;
}
#include "gettoken.h"
#include <QNetworkRequest>
#include <QFile>
#include <QDir>
#include <QTextCodec>
#include <QUrlQuery>
#include <QUrl>
#include <QSslSocket>
getToken::getToken(QObject *parent) : QObject(parent)
{
appID = "1037983989117-p3cllh8ou68c32v9irrh7i9fcdk8egs7.apps.googleusercontent.com";
secretString = "uQQZK2Jb9XOprgYzYjrrWj3s";
qReply = Q_NULLPTR;
accessUrl = "www.googleapis.com/oauth2/v4/token";
qnam = new QNetworkAccessManager(this);
}
void getToken::requestToken(QString code)
{
qDebug()<<"1: "<<code; //very sure this is right code
QUrl setUrl(accessUrl);
QNetworkRequest qRequest(setUrl);
qRequest.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");
connect(qnam,SIGNAL(finished(QNetworkReply *)), this,SLOT(writeFile(QNetworkReply *)));
QByteArray qByte;
qByte.append("code="+code+"&");//very sure this is right code
qByte.append("client_id=1037983989117-p3cllh8ou68c32v9irrh7i9fcdk8egs7.apps.googleusercontent.com&");
qByte.append("client_secret=uQQZK2Jb9XOprgYzYjrrWj3s&");
qByte.append("redirect_uri=http://localhost:8080/cb&");
qByte.append("grant_type=authorization_code");
qDebug() << "***** request url=" << qRequest.url();
qDebug() << "***** request content type header=" << qRequest.ContentTypeHeader;
qDebug() << "***** call POST *****";
qnam->post(qRequest, qByte);
// qnam.post(qRequest,qByte);
qDebug()<<"5";
// qDebug()<<"almost done";
}
void getToken::writeFile(QNetworkReply *mReply)
{
qDebug() << "***** finishedSlot! *****";
// Reading attributes of the reply
// e.g. the HTTP status code
QVariant statusCodeV =
mReply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
// Or the target URL if it was a redirect:
QVariant redirectionTargetUrl =
mReply->attribute(QNetworkRequest::RedirectionTargetAttribute);
// see CS001432 on how to handle this
// no error received?
if (mReply->error() == QNetworkReply::NoError)
{
// read data from QNetworkReply here
// Example 2: Reading bytes form the reply
QByteArray bytes = mReply->readAll(); // bytes
QString stringResponse(bytes); // string
qDebug() << "***** response *****" << stringResponse;
}
// Some http error received
else
{
qDebug() << "***** reply http error =" <<(int) mReply->error();
QByteArray bytes = mReply->readAll(); // bytes
QString stringResponse(bytes); // string
qDebug() << "***** response *****" << stringResponse;
}
// We receive ownership of the reply object
// and therefore need to handle deletion.
delete mReply;
}
To copy to clipboard, switch view to plain text mode
Bookmarks