BigQuery [PHP API] get list of accessible projects - php

I used below PHP code based on the GCP documentation here to fetch a list of all the projects which can be accessed with the selected service account.
require __DIR__ . '/vendor/autoload.php';
putenv('GOOGLE_APPLICATION_CREDENTIALS=acc-key.json');
$client = new Google_Client();
$client->setApplicationName('SampleApp');
$client->useApplicationDefaultCredentials();
$client->addScope('https://www.googleapis.com/auth/cloud-platform');
$service = new Google_Service_Bigquery($client);
$optParams = [];
do {
$response = $service->projects->listProjects($optParams);
foreach ($response['projects'] as $project) {
print_r($project);
}
$optParams['pageToken'] = $response->getNextPageToken();
} while ($optParams['pageToken']);
This does work and fetches only one or two projects from a total of 25 projects which can be accessed with this key.
I confirmed the permission on other projects by accessing them while providing an exclusive project name.
Any hints on what could be the missing part?
EDIT: [Based on Mikhail's answer]
I get below error if I change
$service = new Google_Service_Bigquery($client);
to
$service = new Google_Service_CloudResourceManager($client);
I get below error
PHP Notice: Undefined property: Google_Service_CloudResourceManager::$projects in /tests/ListFullSchema.php on line 18
Notice: Undefined property: Google_Service_CloudResourceManager::$projects in /tests/ListFullSchema.php on line 18
PHP Fatal error: Uncaught Error: Call to a member function listProjects() on null in /tests/ListFullSchema.php:18
Stack trace:
#0 {main}
thrown in /tests/ListFullSchema.php on line 18
Fatal error: Uncaught Error: Call to a member function listProjects() on null in /tests/ListFullSchema.php:18

It is because Cloud Resource Manager v2 doesn't have projects.list method, but v1 does. If you'd like to use Google APIs Client Library for PHP for this task, you can download the v1.1.8 and install it following the instructions here. Then you can run the PHP code like in here.
You should use:
$service = new Google_Service_CloudResourceManager($client);
Alternatively, you can use Python, like in the example. The service version is specified inside the code so you don't need custom installations.

Related

Twilio PHP conversation->message->read , could not sort using provided syntax

Here is the snippet of twilio doc for php to order messages
<?php
// Update the path below to your autoload.php,
// see https://getcomposer.org/doc/01-basic-usage.md
require_once '/path/to/vendor/autoload.php';
use Twilio\Rest\Client;
// Find your Account SID and Auth Token at twilio.com/console
// and set the environment variables. See http://twil.io/secure
$sid = getenv("TWILIO_ACCOUNT_SID");
$token = getenv("TWILIO_AUTH_TOKEN");
$twilio = new Client($sid, $token);
$messages = $twilio->conversations->v1->conversations("CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
->messages
->read(["order" => "desc"], 20);
foreach ($messages as $record) {
print($record->sid);
}
Here is my implementation
$messages = $twilio_client->conversations->v1->conversations($conversation_channel_sid)
->messages->read(["order" => "desc"], 20);
but it was not allowed. it ran into following error:
Fatal error: Uncaught TypeError: Argument 1 passed to
Twilio\Rest\Conversations\V1\Conversation\MessageList::read() must be
of the type int or null, array given, called in
D:\xampp\htdocs\teleupachar2.0\P127_TeliUpachar\chat-module\conversation_test.php
on line 67 and defined in
D:\xampp\htdocs\teleupachar2.0\P127_TeliUpachar\vendor\twilio\sdk\src\Twilio\Rest\Conversations\V1\Conversation\MessageList.php:103
Stack trace: #0
D:\xampp\htdocs\teleupachar2.0\P127_TeliUpachar\chat-module\conversation_test.php(67):
Twilio\Rest\Conversations\V1\Conversation\MessageList->read(Array, 20)
#1 {main} thrown in D:\xampp\htdocs\teleupachar2.0\P127_TeliUpachar\vendor\twilio\sdk\src\Twilio\Rest\Conversations\V1\Conversation\MessageList.php
on line 103
it says it must be of the type int or null. Is there any other ways to filter or sort those messages?
Thank you for your time.
Twilio developer evangelist here.
It turns out that the order parameter is new. In fact it was only released a week ago. The helper libraries are generated every 2 weeks and the next time it will be generated with the latest API definitions is 25th August. In the meantime, the documentation got ahead of the library.
So, look out for the next version of the Twilio PHP library, which should bring the order parameter to the messages resource list and make that example code work for you.

simplesamlphp throwing error after integrating with an APP

Anyone can help with this issue:
I have simplesamlphp instaled and tested successfully with my IdP and SP.
when I integrated the APP following the doc (step 6) https://simplesamlphp.org/docs/1.5/simplesamlphp-sp I started getting errors I will post bellow.
Here is my autoload.php:
require __DIR__.'/../vendor/autoload.php';
require_once('/var/simplesamlphp/lib/_autoload.php');
$as = new SimpleSAML_Auth_Simple ( 'default-sp' );
if ($as->isAuthenticated ()) {
//die ( 'ok' );
} else {
$param = array (
'ReturnTo' => 'http://10.128.240.181/'
);
$as->requireAuth ( $param );
}
$attributes = $as->getAttributes();
print_r($attributes);
$session = SimpleSAML_Session::getSessionFromRequest();
$session->cleanup();
Error output:
Feb 07 16:52:32 simplesamlphp WARNING [CL0a04f709] The class or interface 'SimpleSAML_Auth_Simple' is now using namespaces, please use 'SimpleSAML\Auth\Simple'.
PHP Notice: Undefined index: REQUEST_URI in /var/simplesamlphp/lib/SimpleSAML/Utils/HTTP.php on line 810
PHP Stack trace:
PHP 1. {main}() /var/www/Cachet/bootstrap/autoload.php:0
PHP 2. SimpleSAML\Auth\Simple->requireAuth() /var/www/Cachet/bootstrap/autoload.php:58
PHP 3. SimpleSAML\Auth\Simple->login() /var/simplesamlphp/lib/SimpleSAML/Auth/Simple.php:103
PHP 4. sspmod_saml_Auth_Source_SP->initLogin() /var/simplesamlphp/lib/SimpleSAML/Auth/Simple.php:161
PHP 5. sspmod_saml_Auth_Source_SP->authenticate() /var/simplesamlphp/lib/SimpleSAML/Auth/Source.php:193
PHP 6. sspmod_saml_Auth_Source_SP->startSSO() /var/simplesamlphp/modules/saml/lib/Auth/Source/SP.php:431
PHP 7. sspmod_saml_Auth_Source_SP->startSSO2() /var/simplesamlphp/modules/saml/lib/Auth/Source/SP.php:336
PHP 8. sspmod_saml_Auth_Source_SP->sendSAML2AuthnRequest() /var/simplesamlphp/modules/saml/lib/Auth/Source/SP.php:298
PHP 9. SAML2\HTTPRedirect->send() /var/simplesamlphp/modules/saml/lib/Auth/Source/SP.php:314
PHP 10. SAML2\Compat\Ssp\Container->redirect() /var/simplesamlphp/vendor/simplesamlphp/saml2/src/SAML2/HTTPRedirect.php:83
PHP 11. SimpleSAML_Utilities::redirectTrustedURL() /var/simplesamlphp/vendor/simplesamlphp/saml2/src/SAML2/Compat/Ssp/Container.php:52
PHP 12. SimpleSAML\Utils\HTTP::redirectTrustedURL() /var/simplesamlphp/lib/SimpleSAML/Utilities.php:276
PHP 13. SimpleSAML\Utils\HTTP::normalizeURL() /var/simplesamlphp/lib/SimpleSAML/Utils/HTTP.php:968
PHP 14. SimpleSAML\Utils\HTTP::getSelfURL() /var/simplesamlphp/lib/SimpleSAML/Utils/HTTP.php:889
****PHP Fatal error: Uncaught SimpleSAML_Error_UnserializableException: Unable to parse base url: http://localhost in /var/simplesamlphp/lib/SimpleSAML/Auth/Source.php:197****
Stack trace:
#0 /var/simplesamlphp/lib/SimpleSAML/Auth/Simple.php(161): SimpleSAML_Auth_Source->initLogin('http://10.128.2...', NULL, Array)
#1 /var/simplesamlphp/lib/SimpleSAML/Auth/Simple.php(103): SimpleSAML\Auth\Simple->login(Array)
#2 /var/www/Cachet/bootstrap/autoload.php(58): SimpleSAML\Auth\Simple->requireAuth(Array)
#3 {main}
thrown in /var/simplesamlphp/lib/SimpleSAML/Auth/Source.php on line 197
Anyone has an idea what could be the cause of this error ? I am a newbie in php and SAML,
I think there are two issues. The first one is:
Feb 07 16:52:32 simplesamlphp WARNING [CL0a04f709] The class or interface 'SimpleSAML_Auth_Simple' is now using namespaces, please use 'SimpleSAML\Auth\Simple'.
It seems you are following an old documentation and using a recent version of SimpleSAMLphp. You will have to use $as = new \SimpleSAML\Auth\Simple('default-sp'); instead of $as = new SimpleSAML_Auth_Simple ( 'default-sp' );
If you are using the latest stable version of SimpleSAMLphp, please follow https://simplesamlphp.org/docs/stable/simplesamlphp-sp
The second issue is
****PHP Fatal error: Uncaught SimpleSAML_Error_UnserializableException: Unable to parse base url: http://localhost in /var/simplesamlphp/lib/SimpleSAML/Auth/Source.php:197****
This is more related to the configuration (This is a wild guess). Can you provide more info on your web server (IIS/Apache/nginx)? I think IIS has some known issues: https://github.com/simplesamlphp/simplesamlphp/issues/540

Coinbase API for PHP: Undefined index: pagination returned by $client->getAccounts()

After clean install of the Coinbase API for PHP on my Xubuntu 16.04 (composer require coinbase/coinbase) I followed the Coinbase documentation and used the following commands to access my wallet:
<?php
require 'vendor/autoload.php';
use Coinbase\Wallet\Client;
use Coinbase\Wallet\Configuration;
$apiKey = '********';
$apiSecret = '*********';
$configuration = Configuration::apiKey($apiKey, $apiSecret);
$client = Client::create($configuration);
$accounts = $client->getAccounts();
...
...
The function getAccounts always returns:
PHP Notice: Undefined index: pagination in /home/willem/coinbase/vendor/coinbase/coinbase/src/Mapper.php on line 490
PHP Stack trace:
PHP 1. {main}() /home/willem/coinbase/newfile.php:0
PHP 2. Coinbase\Wallet\Client->getAccounts() /home/willem/coinbase/newfile.php:14
PHP 3. Coinbase\Wallet\Client->getAndMapCollection() /home/willem/coinbase/vendor/coinbase/coinbase/src/Client.php:215
PHP 4. Coinbase\Wallet\Mapper->toAccounts() /home/willem/coinbase/vendor/coinbase/coinbase/src/Client.php:834
PHP 5. Coinbase\Wallet\Mapper->toCollection() /home/willem/coinbase/vendor/coinbase/coinbase/src/Mapper.php:59
PHP Notice: Undefined index: pagination in /home/willem/coinbase/vendor/coinbase/coinbase/src/Mapper.php on line 491
PHP Stack trace:
PHP 1. {main}() /home/willem/coinbase/newfile.php:0
PHP 2. Coinbase\Wallet\Client->getAccounts() /home/willem/coinbase/newfile.php:14
PHP 3. Coinbase\Wallet\Client->getAndMapCollection() /home/willem/coinbase/vendor/coinbase/coinbase/src/Client.php:215
PHP 4. Coinbase\Wallet\Mapper->toAccounts() /home/willem/coinbase/vendor/coinbase/coinbase/src/Client.php:834
PHP 5. Coinbase\Wallet\Mapper->toCollection() /home/willem/coinbase/vendor/coinbase/coinbase/src/Mapper.php:59
Despite the error, the account information is retrieved into $accounts.
Obviously I now use 2>/dev/null but I'd rather not.
Any solution is highly appreciated !
The suggested line of code indeed suppresses the 'notice', as per Alex's comment.
I'm a happy coder again :)

Google Calendar API fetch available event colors resulting in undefined method fatal error

I'm trying to fetch the available event colors for a calendar, and I'm getting this error:
PHP Fatal error: Call to undefined method Google_Service_Calendar_Colors_Resource::getEvent()
From the last line of this code:
$client = new Google_Client();
$client->setClientId($configArray['google']['calsync']['client_id']);
$client->setClientSecret($configArray['google']['calsync']['client_secret']);
$client->setRedirectUri($configArray['google']['calsync']['redirect_uri']);
$client->setUseBatch(true);
$client->setScopes('calendar');
$client->setAccessToken("ACCESSTOKEN");
$cal_client = new Google_Service_Calendar($client);
$colors = $cal_client->colors->get();
$event_colors = $colors->getEvent();
Why is this happening? What can I do to catch this error? I'm following the example used on: https://developers.google.com/google-apps/calendar/v3/reference/colors/get
I figured out the problem. The following code, which sets the client to batch requests is causing the fatal error. This code:
$client->setUseBatch(true);
Should not be present, or it should be:
$client->setUseBatch(false);

PHP CallFire SDK - Authentication and Request/Response problems

Disclaimer: php novice student working really hard here.
I'm trying to build an app that interacts with the CallFire API.
At this point all I'm trying to achieve is establishing a communication with the API and succesfully getting a request/response cycle going.
I have installed the Callfire SDK dependency with Composer on my XAMPP local server. I want to get this very basic example running: it instantiates the API, sends a request and parses the response.
<?php
use CallFire\Api\Rest\Request;
require 'vendor/autoload.php';
$client = CallFire\Api\Client::Rest("<api-login>", "<api-password>", "Broadcast"); //I'm using my valid CallFire API keys here.
$request = new Request\QueryBroadcasts;
$response = $client->QueryBroadcasts($request); //LINE 10
$broadcasts = $client::response($response);
foreach($broadcasts as $broadcast) {
var_dump($broadcast);
}
I put this code in a index.php file and when I run it in my browser I get:
Notice: Trying to get property of non-object in C:\xampp\htdocs\cfireapi1\vendor\callfire\php-sdk\src\CallFire\Api\Rest\Response.php on line 39
Notice: Trying to get property of non-object in C:\xampp\htdocs\cfireapi1\vendor\callfire\php-sdk\src\CallFire\Api\Rest\Response.php on line 39
Fatal error: Uncaught exception 'UnexpectedValueException' with message 'Response type not recognized' in C:\xampp\htdocs\cfireapi1\vendor\callfire\php-sdk\src\CallFire\Api\Rest\Response.php:50
Stack trace: #0 C:\xampp\htdocs\cfireapi1\vendor\callfire\php-sdk\src\CallFire\Api\Rest\AbstractClient.php(59): CallFire\Api\Rest\Response::fromXml(false)
#1 C:\xampp\htdocs\cfireapi1\index.php(10): CallFire\Api\Rest\AbstractClient::response(false)
#2 {main} thrown in C:\xampp\htdocs\cfireapi1\vendor\callfire\php-sdk\src\CallFire\Api\Rest\Response.php on line 50
Line 59 in AbstractClient.php is
public static function response($data, $type = self::FORMAT_XML)
{
if (is_string($data) && strlen($data) === 0) {
return true; // Many operations return an empty string for success
}
switch ($type) {
case static::FORMAT_XML:
return AbstractResponse::fromXml($data); // LINE 59
case static::FORMAT_JSON:
return AbstractResponse::fromJson($data);
}
throw new InvalidArgumentException("Type must be 'xml' or 'json'");
}
And lines 39 and 50 in Response.php are
public static function fromXml($document)
{
$document = static::getXmlDocument($document);
// return print_r($document);
switch ($document->firstChild->nodeName) { // LINE 39 error for this line is Notice: Trying to get property of non-object
case 'r:ResourceList':
return Response\ResourceList::fromXml($document);
case 'r:Resource':
return Response\Resource::fromXml($document);
case 'r:ResourceReference':
return Response\ResourceReference::fromXml($document);
case 'r:ResourceException':
return Response\ResourceException::fromXml($document);
}
throw new UnexpectedValueException('Response type not recognized'); // LINE 50: this is what throws the fatal error exception
}
Line 10 is highlighted in the example code.
So I know that the API is sending back an response with an unidentified type. Why, if I'm literally using the SDK and one of the basic examples in the documentation? How can I check the response that I'm getting to see what it is exactly? I tried printing the response with r_print inside the Response.php file but I got a "1" printed in the browser. Which makes not sense to me. Is it because I'm running this in the browser?
SOLVED: Somehow running the program in the localhost environment was causing the problem. Everything worked once I uploaded the application into my server.

Categories