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
Developer
Posts: 265
Registered: ‎01-02-2011
My Device: Z30, PlayBook
My Carrier: AT&T
Accepted Solution

Send a Form Post request in QML

I'm trying to get the final step of Oauth 2 to work.  I need to send the secret, key and code to the provider url, but I can't send it as the WebView url and using PostMessage doesn't return anything to the OnMessageReceived function.

 

On the QML page, I have a button that sets the initial URL for the WebView.

When the URL changes, I check for the return code.

I parse out that code to send for the Token, but that's when I get the error that I can't send a "get".

 

 

Any suggestions?

_________________
Meetup for BlackBerry 10
Meetup Search Tool - not available at the moment
Highlighted
Developer
Posts: 85
Registered: ‎06-22-2013
My Device: N/A
My Carrier: N/A

Re: Send a Form Post request in QML

Try doing this using C++ , It always works with me using C++ Not in QML

 

Developer
Posts: 265
Registered: ‎01-02-2011
My Device: Z30, PlayBook
My Carrier: AT&T

Re: Send a Form Post request in QML

Okay.  I went that route.  Thanks for the pointer.

I found some code and modified it for what I needed.

 

After using the Webview to get the initial request key, I call a C++ function to send an access request.

I pass in the url for the access request, my apps consumer key and secret, the returned code from the initial request, the redirect url (required for the OAuth I'm using, but does nothing) and the grant type.

 

void HelloWorldServices::getAccess(const QString &urlString, const QString &consumerKey, const QString &consumerSecret, const QString &code, const QString &redirectURI, const QString &grantType)
{
	QUrl mMultiPart;
	mMultiPart.setUrl(urlString);
	// post items for the Oauth Access I am using
	mMultiPart.addQueryItem("client_id", consumerKey);
	mMultiPart.addQueryItem("client_secret", consumerSecret);
	mMultiPart.addQueryItem("grant_type",  grantType);
	mMultiPart.addQueryItem("redirect_uri", redirectURI);
	mMultiPart.addQueryItem("code", code);
	// blank outrequest
	QString outRequest = "";

	QNetworkAccessManager *networkManager = new QNetworkAccessManager(this);
	// Setting mutipart query to request
	QNetworkRequest request(mMultiPart);
	request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
	// ssl setup - works with or without
	QSslConfiguration config = request.sslConfiguration();
	config.setPeerVerifyMode(QSslSocket::VerifyNone);
	config.setProtocol(QSsl::TlsV1);
    request.setSslConfiguration(config);

	if (networkManager)
	{
		QNetworkReply *reply = networkManager->post(request, outRequest.toAscii());
		bool ok = connect(reply, SIGNAL(finished()), this, SLOT(onGetReply()));

		Q_ASSERT(ok);
		Q_UNUSED(ok);

		if (reply) {
			qDebug() << "Reply from server is " << reply->readBufferSize();
		}
	}
}

 When the reply happens, then I can parse out the JSON response

void HelloWorldServices::onGetReply()
{
	QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
	QString response;

	if (reply)
	{
		if (reply->error() == QNetworkReply::NoError) {
			int available = reply->bytesAvailable();
			if (available > 0) {
				QByteArray buffer(reply->readAll());
				response = QString(buffer);
			} else {
				response = "Zero bytes in response";
			}
		} else {
			int httpStatus = reply->attribute(
					QNetworkRequest::HttpStatusCodeAttribute).toInt();
			response = "Error and the code is "
							+ reply->attribute(
									QNetworkRequest::HttpStatusCodeAttribute).toString()
							+ "  And the Error string is :: "
							+ reply->errorString() + "\n";
		}
		reply->deleteLater();
	} else {
		response = "Response comes out to be null";
	}

	if ((response.trimmed().isEmpty()))
	{
		response = "Empty Response";
	}
	qDebug() << "Response String is :: " << response << " \n";

	if (!response.isNull())
	{
		qDebug() << "Start JSON";
		JsonDataAccess jda;
		QVariant mainList = jda.loadFromBuffer(response);
		qDebug() << mainList.toStringList();
		qDebug() << mainList.toMap().value("access_token");
		qDebug() << mainList.toMap().value("token_type");
		qDebug() << mainList.toMap().value("expires_in");
		qDebug() << mainList.toMap().value("refresh_token");
	}
}

 

_________________
Meetup for BlackBerry 10
Meetup Search Tool - not available at the moment
Developer
Posts: 265
Registered: ‎01-02-2011
My Device: Z30, PlayBook
My Carrier: AT&T

Re: Send a Form Post request in QML

And here is what I did to get things started.

 

I load the URL into the WebView.

I check the onURLChange -- the other options didn't work

I check to see if the return value contains "code", because I get more than one URL.

When I get the code, then I call the function to complete Oauth2.

import bb.cascades 1.0
import HelloWorldServices 1.0

Page {
    
    property string returnCode;
    property string consumerKey: "123456789abcdef";
    property string consumerSecret: "abcdef987654321";
    property string redirectURI: "http://mysite.com/oauth/";
    property int urlChange: 0;
    property bool codeReceived: false;
    property bool accessSent: false;
    property bool accessReceived: false;
    
    Container {
        Label {
            id: lHelloLabel
            // Localized text with the dynamic translation and locale updates support
            text: qsTr("Hello World") + Retranslate.onLocaleOrLanguageChanged
            textStyle.base: SystemDefaults.TextStyles.BigText
        }
        Button {
            id: btnGetHello
            text: "Get OAuth Access"
            
            onClicked: {
                startOAuth();
            }
            
            function startOAuth() {
                // open the authorzation url
                var url = 'https://secure.site.com/oauth2/authorize?'
                + 'client_id=' + consumerKey 
                + '&response_type=code' 
                + '&redirect_uri=' + redirectURI;
                webWindow.url = url;
            }
        }
        
        ScrollView {
            Container {
                WebView {
                    id: webWindow
                    
                    onUrlChanged: {
                       var returnURL = webWindow.url.toString();
                        // check to see if code has comeback before
                        if(!codeReceived) {
                            // Past initial login request
                            if(returnURL.indexOf("code")) {
                                // successful return
                                var startTrim = returnURL.indexOf("code") + 5;
                                var endTrim = returnURL.indexOf("&");
                                
                                if(endTrim > startTrim) {
                                    returnCode = returnURL.substring(startTrim, endTrim);
                                }
                                if(returnCode.length > 1) {
                                    // set code recieved to true
                                    codeReceived = true;
                                    if(!accessSent) {
                                        accessSent = true;
                                        // call Access function to complete OAuth
                                        getAccess();
                                    }
                                }
                                
                            } else if(returnURL.indexOf("invalid_request") != -1) {
                                taHelloResults2.setText("The request was malformed or missing parameters");
                            } else if(returnURL.indexOf("unauthorized_client") != -1) {
                                taHelloResults2.setText("The client is not authorized");
                            } else if(returnURL.indexOf("access_denied") != -1) {
                                taHelloResults2.setText("The user denied the request for authorization");
                                // do some sort of action about user not being able to access data
                            } else if(returnURL.indexOf("unsupported_response_type") != -1) {
                                taHelloResults2.setText("Meetup doesn't support the provided response_type");
                            } else if(returnURL.indexOf("invalid_grant") != -1) {
                                taHelloResults2.setText("The provided code was invalid");
                            } else if(returnURL.indexOf("unsupported_response_type") != -1) {
                                taHelloResults2.setText("Meetup does not support the provided grant type");
                            } else {
                                // other
                                taHelloResults2.setText("Other data" + returnURL);
                            }
                        }
                        
                    }
                    
                    function getAccess() {
                        var accessURL = String("https://secure.site.com/oauth2/access");
                        hws.getAccess(accessURL, consumerKey, consumerSecret, returnCode, redirectURI, "authorization_code");
                    }                    
                }
                TextArea {
                    id: taHelloResults1
                    minHeight: 50
                }
                
                TextArea {
                    id: taHelloResults2
                    minHeight: 50
                }
            }
            
        }
    }
    attachedObjects: [
        HelloWorldServices {
            id: hws
        }
        
    ]
}

 

_________________
Meetup for BlackBerry 10
Meetup Search Tool - not available at the moment