All Requests return 403 Unauthorized - google-cloud-datastore

I'm trying to work with the cloud datatstore - I've created an appengine project, which is viewable at https://cloud.google.com/console#/.
Through the cloud console, I've navigated to APIs, and turned off and back on the datastore API. I've then registered an app (web application, generic) and generated a certificate. I've ensured that the email generated from that is in the 'Teams' list with 'Can Edit' permission.
After all this, any requests I generate are returning 403. Requests to other APIs, such as the prediction API, are working okay. The datastore is returning 403 both from my codebase (using php api) or from the api explorer.
Below is a dump of the HTTP Request generated from the PHP client. Any help would be greatly appreciated!
object(Google_HttpRequest)[15]
private 'batchHeaders' =>
array (size=4)
'Content-Type' => string 'application/http' (length=16)
'Content-Transfer-Encoding' => string 'binary' (length=6)
'MIME-Version' => string '1.0' (length=3)
'Content-Length' => string '' (length=0)
protected 'url' => string 'https://www.googleapis.com/datastore/v1beta1/datasets/madlab-sandbox/lookup' (length=75)
protected 'requestMethod' => string 'POST' (length=4)
protected 'requestHeaders' =>
array (size=3)
'content-type' => string 'application/json; charset=UTF-8' (length=31)
'content-length' => int 62
'authorization' => string 'Bearer ya29.AHES6ZSfJmWHLNIrPwQA7wZ3miGCMgxvmUmYl65mfw9J2_v8KgmLMyJV' (length=68)
protected 'postBody' => string '{"keys":[{"path":[{"id":"5629499534213120","kind":"topic"}]}]}' (length=62)
protected 'userAgent' => string 'test google-api-php-client/0.6.4' (length=32)
protected 'responseHttpCode' => int 403
protected 'responseHeaders' =>
array (size=9)
'content-type' => string 'application/json; charset=UTF-8' (length=31)
'date' => string 'Fri, 26 Jul 2013 15:26:13 GMT' (length=29)
'expires' => string 'Fri, 26 Jul 2013 15:26:13 GMT' (length=29)
'cache-control' => string 'private, max-age=0' (length=18)
'x-content-type-options' => string 'nosniff' (length=7)
'x-frame-options' => string 'SAMEORIGIN' (length=10)
'x-xss-protection' => string '1; mode=block' (length=13)
'server' => string 'GSE' (length=3)
'transfer-encoding' => string 'chunked' (length=7)
protected 'responseBody' => string '{
"error": {
"errors": [
{
"domain": "global",
"reason": "PERMISSION_DENIED",
"message": "Unauthorized."
}
],
"code": 403,
"message": "Unauthorized."
}
}
' (length=182)
public 'accessKey' => null

To work around this issue:
Visit the Google Cloud Console.
Click on your existing Cloud project.
Click on "Teams" under the settings menu.
Find the email address corresponding to your certificate (it will be a ~45 character string followed by #developer.gserviceaccount.com).
Remove and then re-add this email address as a member of your project.
I've filed this on the Cloud Datastore public issue tracker:
https://github.com/GoogleCloudPlatform/google-cloud-datastore/issues/10
Please check there for updates.

Related

HTTP/2 400 returned for URL when using symfoy HTTP Client to make post request

I want to transform a http post request tested with post man to symfony action :
I want to transform the payload to a json array in symfony to send data to url :
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\Routing\Annotation\Route;
class PushNotificationController extends AbstractController
{
/**
* #Route("/api/push_notification", name="push_notification")
*/
public function index()
{
$httpClient = HttpClient::create();
$response = $httpClient->request('POST', 'https://fcm.googleapis.com/fcm/send', [
'headers' => [
// 'Accept' => 'application/json',
// "Content-Type" => "application/json; charset=UTF-8",
'Authorization' => 'Bearer token'
],
'json' => [
'notification' => [
'title' => 'Portugal vs. Denmark',
'message' => 'My Notification Message',
'body' => '5 to 1',
],
'token' => 'token'
],
]);
$statusCode = $response->getStatusCode();
// $statusCode = 200
$contentType = $response->getHeaders()['content-type'][0];
// $contentType = 'application/json'
$content = $response->getContent();
// $content = '{"id":521583, "name":"symfony-docs", ...}'
$content = $response->toArray();
// $content = ['id' => 521583, 'name' => 'symfony-docs', ...]
return $content;
}
}
I got this error :
I think it's an error about the payload . any suggestions please ?
Error 400 Invalid Json input:
Only applies for JSON requests. Indicates that the request could not
be parsed as JSON, or it contained invalid fields (for instance,
passing a string where a number was expected). The exact failure
reason is described in the response and the problem should be
addressed before the request can be retried.
so i guess you need to review your json that you sent.
also , you can use some of the FCM Bundle from Knp or git.

HttpClient Component Authentication

I need to to be authentificate when create a HTTP request, in the documentation
$httpClient = HttpClient::create([
'auth_basic' => ['user#gmail.com', '45#8888'],
]);
$httpClient = HttpClient::create([
// HTTP Basic authentication with only the username and not a password
'auth_basic' => ['the-username'],
// HTTP Basic authentication with a username and a password
'auth_basic' => ['the-username', 'the-password'],
// HTTP Bearer authentication (also called token authentication)
'auth_bearer' => 'the-bearer-token',
]);
So when write
$response = $httpClient->request('GET', 'https://...', [
// use a different HTTP Basic authentication only for this request
'auth_basic' => ['the-username', 'the-password'],
// ...
]);
An exception is throwed
Symfony\Component\HttpClient\Exception\ClientException: HTTP/1.1 400
Bad Request returned for "http://site.test/login_check".
Is the exeption throwed because i haven't post the field user[_token] ?
If yes how to generate it and add to the request ?
If anyone has already logged in with this component please give me the code :)
I used that and it works
Try, for Authentication and Request:
$httpClient = HttpClient::create();
$uri = 'http://test:xxxx/login_check';
$response = $httpClient->request('POST', $uri, [
'headers' => [
'Content-Type' => 'application/json',
],
'json' => [
'username' => 'jhon.doe#gmail.com',
'password' => 'jhon.doe'
],
]);
$statusCode = $response->getStatusCode();
if($statusCode == 200) {
$content = $response->getContent();
dd($content);
}
// JWT Request Http
$uri = 'http://test.xxx/api/xxxx';
$response = $httpClient->request('GET', $uri, [
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer '.TOKEN
]
]);
$statusCode = $response->getStatusCode();
$content = $response->getContent();
dd($content);
//the easiest
$httpClient->request('POST', $url, [
'auth_bearer' => $jwt,
'body' => $data,
])->getContent();

ZF3 development of RestAPI: handling Post request for

I am new to Zf3, and developing Email Restful API.
having difficulties in handling request. I can't get POST parameters.i think there is some problem in module.config.php > 'router'
Questions
1 How can I get POST variables from request.
2 I found it when I request page (using Postman)
- if I passes ID, controller call get($id) method
- if I POST variables controller calls Create($data) function.
--is this always the case ? and is it good to write zendEmail code inside create($data) method
.
bwsmail controller
(create function)
public function create($data)
{
echo $this->getRequest()->getPost('id', "no value");
// Output
// this returns no value
echo $this->getRequest();
// Output
//POST http://localhost:8080/bwsmail/mail?id=123456789 HTTP/1.1
//Cookie: PHPSESSID=p5i7o8lm2ed0iocdhkc8jvos01
//Cache-Control: no-cache
//Postman-Token: ccaf5b37-02a2-4537-bf3f-c1dc419d8ceb
//User-Agent: PostmanRuntime/7.6.1
//Accept: */*
//Host: localhost:8080
//Accept-Encoding: gzip, deflate
//Content-Length: 0
//Connection: keep-alive
$response = $this->getResponseWithHeader()->setContent( __METHOD__.' create new item of data :<b>'.'</b>');
return $response;
}
module.config.php (route)
'bwsmail' => [
'type' => Literal::class,
'options' => [
'route' => '/bwsmail',
'defaults' => [
'controller' => Controller\BwsMailController::class,
]
],
'may_terminate' => true,
'child_routes' => [
'mail' => [
'type' => 'segment',
'options' => [
'route' => '/mail[/:id]',
'constraints'=>
[
'id' => '[0-9a-zA-Z]+',
],
'defaults' => [
'controller' => Controller\BwsMailController::class,
]
],
],
],
],
if BwsMailController extends AbstractController or AbstractRestfulController you can simply: $this->params()->fromRoute() or ->fromQuery() if you're using a get string. For example:
$routeParamId = $this->params()->fromRoute('id', null);
This gets the parameter id as it is defined in the route configuration. If it's not set, it will default to null.
If you have a GET url, something like: site.com/mail?id=123, then you do:
$getParamId = $this->params()->fromQuery('id', null); // sets '123' in var
More options are available, have a good read of the routing documentation.

symfony 1.4 - all routing values being deleted when one is set in factories.yml

for development/ production purposes I am setting this in factories.yml
all:
routing:
class: sfPatternRouting
param:
generate_shortest_url: true
logging: false
extra_parameters_as_query_string: true
context:
host: www.mydomain.com
Unfortunately this results in everything that would normally be in the routing context being overwritten so things like prefix, referrer etc are missing so:
WITHOUT this context being set in factories.yml.
when you look at the routing you get a full set of context variables
object(sfRoute)[31]
protected 'isBound' => boolean true
protected 'context' =>
array
'path_info' => string '/login' (length=6)
'prefix' => string '/clients' (length=8)
'method' => string 'GET' (length=3)
'format' => null
'host' => string 'www.domain.com'
'is_secure' => boolean false
'request_uri' => string 'http:/www.domain.com/clients/login'
protected 'parameters' =>
array
'module' => string 'sfGuardAuth' (length=11)
........
But WITH THIS factories.yml setting all the context variables except the host (which is what I explicitly set in factories.yml) is missing
object(sfRoute)[47]
protected 'isBound' => boolean true
protected 'context' =>
array
'host' => string 'www.newdomain.com'
protected 'parameters' =>
array
'module' => string 'sfGuardAuth' (length=11)
.....
Does anyone know a way to set this 'host' context variable without it wiping everything else out?

Getting Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException

I'm sending a PUT request that handles input data and updates a record, but I get the above response. The problem doesn't seem to be the route, however, because if I do dd($user) after the $user = User::whereId($id)->firstOrFail(); line, I get the object returned correctly.
Yet, when it comes time to validate it, it throws this error.
# routes
Route::resource('users', 'UsersController', ['only' => ['index', 'show', 'update']]);
# api call
PUT /users/2
# controller
public function update($id)
{
$user = User::whereId($id)->firstOrFail();
$input = Input::all();
$this->userForm->validate($input);
$user->fill($input)->save();
return $user->toJson();
}
# userForm class
<?php namespace Olp\Forms;
use Laracasts\Validation\FormValidator;
class UserForm extends FormValidator {
protected $rules = [
'email' => 'required|email|unique:users',
'password' => 'required',
'password_confirmation' => 'required|same:password',
'firstname' => 'required',
'lastname' => 'required',
'schoolname' => 'required',
'address1' => 'required',
'address2' => 'required',
'postcode' => 'required',
'city' => 'required'
];
}
and in my UserController:
use Olp\Forms\UserForm;
class UsersController extends \BaseController {
function __construct(UserForm $userForm)
{
$this->userForm = $userForm;
}
I'm not a Laravel guy, but a quick check on their documentation indicates that Resource Controllers support PUT in addition to other HTTP verbs.
I was not able to figure out how to add HTTP verb support to an arbitrary action, but this indicates that you can name a controller action prefixed with the verb it responds to
public function putUpdate($id)

Resources