Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Contributor
Posts: 36
Registered: ‎07-05-2012
My Device: 9900 & dev alpha
My Carrier: Play - P4

Login to google services

Hi,

I'm trying to create app which is using google services So I wrote this code. It displays auth token but then I want to get some data from google service I get 401.

#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>

#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QUrl>
#include <QDebug>
#include <QSslConfiguration>
#include <QNetworkCookie>

#include "app.hpp"

using namespace bb::cascades;

App::App()
{
    QmlDocument *qml = QmlDocument::create("main.qml");
    qml->setContextProperty("cs", this);

    AbstractPane *root = qml->createRootNode<AbstractPane>();
    Application::setScene(root);


    manager = new QNetworkAccessManager(this);
	QSslConfiguration sslconf;
	sslconf.setPeerVerifyMode(QSslSocket::VerifyNone);

    QNetworkRequest request(QUrl("https://www.google.com/accounts/ClientLogin"));
    request.setSslConfiguration(sslconf);
    request.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");


    QUrl params;

    params.addQueryItem("Email", "#" );
    params.addQueryItem("Passwd", "#" );
    params.addQueryItem("accountType", "HOSTED_OR_GOOGLE" );
    params.addQueryItem("service", "ac2dm" );
    params.addQueryItem("Source", "myAppPackageName" );

    QObject::connect(manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(loginCallback(QNetworkReply *)));
    QObject::connect(this, SIGNAL(userLoggedIn()), this, SLOT(getNewsFeed()));

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


}

void App::getNewsFeed(){
	QSslConfiguration sslconf;
	sslconf.setPeerVerifyMode(QSslSocket::VerifyNone);

    QNetworkRequest request(QUrl("http://www.google.com/reader/atom/"));
    request.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");
    request.setSslConfiguration(sslconf);

    QString headerValude("GoogleLogin auth="+authValue);
    request.setRawHeader(QByteArray("Authorization"), headerValude.toAscii());

    QObject::connect(manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(newsFeedCallback(QNetworkReply *)));
    QObject::disconnect(manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(loginCallback(QNetworkReply *)));

    manager->get(request);

}
void App::newsFeedCallback(QNetworkReply* reply){
	qDebug() << "get" << reply->isFinished();
	qDebug() << reply->readAll();
}

void App::loginCallback(QNetworkReply *reply){
	QString response(reply->readAll());
	qDebug() << response;
	authValue = response.split("\n").at(2).split("=").at(1);
	emit(userLoggedIn());

}

 

BlackBerry Development Advisor
Posts: 35
Registered: ‎08-01-2012
My Device: BB10 alpha-BB10 developer
My Carrier: T-Mobile

Re: Login to google services

 

This looks like a problem of the usage of google service API itself instead of cascades c++ code problem.

 

 

1) I tried you code with "mail" service and its atom feed, it worked.

 

e.g.:

 

change this line:

params.addQueryItem("service", "ac2dm" );

 

to be

 

params.addQueryItem("service", "mail" );

 

also 

 

changed

 

QNetworkRequest request(QUrl("http://www.google.com/reader/atom/"));

to be

 

QNetworkRequest request(QUrl("https://mail.google.com/mail/feed/atom/"));

 

 

Then,

I successfully got the list for mails

 

 

2) from the google documentation:

https://developers.google.com/gdata/faq#clientlogin

 

 "ac2dm" seems not on the official list for service name of ClientLogin API

 

 

3) I changed the "ac2dm" to "reader"

but keep   QNetworkRequest request(QUrl("https://www.google.com/reader/atom/"));

 

I was able to get the list of feeds.

 

 

4) a friendly warning: google ClientLogin API is deprecated.

https://developers.google.com/accounts/docs/AuthForInstalledApps