Strukov Andrey
Strukov Andrey

Reputation: 11

Getting error "302" during gmail api authentication

Good day, I use gmail api authentication in my app to send emails. Today I've started to get "302" error. It was all fine at the beginning of April when I last time used my app.

void DownloaderGmailAPI::getCode(){

QString FileName = "client_secret______.json";
QFile jsonFile(FileName);
if (!jsonFile.open(QIODevice::ReadOnly))
{
    return;
}
QByteArray saveData = jsonFile.readAll();
jsonFile.close();

QJsonDocument jsonDocument(QJsonDocument::fromJson(saveData));
const auto object = jsonDocument.object();
const auto settingsObject = object["web"].toObject();
const QUrl authUri(settingsObject["auth_uri"].toString());
const auto clientId = settingsObject["client_id"].toString();
const QUrl tokenUri(settingsObject["token_uri"].toString());
const auto clientSecret(settingsObject["client_secret"].toString());
const auto redirectUris = settingsObject["redirect_uris"].toArray();
const QUrl redirectUri(redirectUris[0].toString()); // Get the first URI
const auto port = static_cast<quint16>(redirectUri.port()); // Get the port

QUrl url;
url.setScheme("https");
url.setHost("accounts.google.com");
url.setPath("/o/oauth2/auth");

QUrlQuery params;
params.addQueryItem("scope", "https://www.googleapis.com/auth/gmail.readonly https://www.googleapis.com/auth/gmail.send");
params.addQueryItem("redirect_uri", "http://127.0.0.1:8080");
params.addQueryItem("response_type","code");
params.addQueryItem("client_id", clientId);


url.setQuery(params);
if(checkUrl(url)){
    QNetworkRequest request;    // 
    request.setUrl(url);
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
    manager->get(request);      // 
}}

Maybe I've not seen some updates in address changing or smth else?

Upvotes: 0

Views: 437

Answers (1)

Strukov Andrey
Strukov Andrey

Reputation: 11

Early I got code 200 with redirect url in body. I add getting location from header reply and it helps.

QString location = reply->header(QNetworkRequest::LocationHeader).toString();
QDesktopServices::openUrl(QUrl(location));

Upvotes: 1

Related Questions