Using Google Appengine Taskqueue REST API in Playframework 2.5

2 Jan 2017

The crucial part is to set the header Authorization correctly. Therefore, we need to include google-api-client first. The below lines should be added to build.sbt:

libraryDependencies ++= Seq( ws, "" % "google-api-client" % "1.22.0" )

Please get the service account's credentials in JSON format. And please configure queue.yaml. Here's an example of queue.yaml:

queue: - name: my-pull-queue mode: pull acl: - writer_email:

The writer_email is essential. Now you can deploy queue.yaml with gcloud app deploy queue.yaml.

And you can use the below Scala code to consume Taskqueue REST API:

import import import org.asynchttpclient.{Request, RequestBuilderBase, SignatureCalculator} import play.api.libs.json._ import{WSClient, WSSignatureCalculator} import scala.concurrent.Await import scala.concurrent.duration.Duration import scala.collection.JavaConversions._ val credentials = GoogleCredential .fromStream(new FileInputStream("./credentials.json")) .createScoped(Seq( "", "" )) val result = Await.result( ws .url("") .sign(new SignatureCalculator with WSSignatureCalculator { def calculateAndAddSignature( request: Request, requestBuilder: RequestBuilderBase[_] ): Unit = { requestBuilder.setHeader("Authorization", "Bearer " + credentials.getAccessToken) } }) .post(Json.obj( "payloadBase64" -> "sdagsadg", "kind" -> "taskqueues#task", "queueName" -> "projects/s~test-tanin-selenium/taskqueues/my-pull-queue" )), Duration.apply(10, scala.concurrent.duration.SECONDS))

Please note that test-tanin-selenium is the project name and my-pull-queue is the queue name. The s~ prefix is also essential. (If you project is based in Europe, your prefix is e~.

Give it a kudos