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, "com.google.api-client" % "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: your-service-account@appspot.gserviceaccount.com

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 java.io.FileInputStream import com.google.api.client.googleapis.auth.oauth2.GoogleCredential import org.asynchttpclient.{Request, RequestBuilderBase, SignatureCalculator} import play.api.libs.json._ import play.api.libs.ws.{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( "https://www.googleapis.com/auth/taskqueue", "https://www.googleapis.com/auth/taskqueue.consumer" )) val result = Await.result( ws .url("https://www.googleapis.com/taskqueue/v1beta2/projects/s~test-tanin-selenium/taskqueues/my-pull-queue/tasks") .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