I am setting up the google cloud client library from the below url:
https://cloud.google.com/bigquery/docs/reference/libraries#client-libraries-usage-php
I have created a key from the given url:
https://console.cloud.google.com/apis/credentials/
and set it up in the environment variable.
but i am getting the below error:
Please help.
it work on command prompt something like:
php yourphpfilename.php
also you have an alternate way to pass the key file:
$credentialsFile = '[PATH_TO_JSON_KEY_FILE]';
# Instantiates a client
$bigquery = new BigQueryClient([
'projectId' => $projectId,
'keyFilePath' => $credentialsFile
]);
Hope it will help you.
Related
I put my php code in GCE and wanna to modify google sheets.
Google told me that i don't have to apply a extra credential by using GCP>API because there's a strategy called Application Default Credentials (ADC) will find application's credentials.
I first check the environment variable "GOOGLE_APPLICATION_CREDENTIALS" in GCE server but it's empty.
Then i follow this tutorial https://cloud.google.com/docs/authentication/production?hl=zh_TW#auth-cloud-implicit-php and install this:
composer require google/cloud-storage
I tried the code under and got some error.
namespace Google\Cloud\Samples\Auth;
// Imports GCECredentials and the Cloud Storage client library.
use Google\Auth\Credentials\GCECredentials;
use Google\Cloud\Storage\StorageClient;
function auth_cloud_explicit_compute_engine($projectId)
{
$gceCredentials = new GCECredentials();
$config = [
'projectId' => $projectId,
'credentialsFetcher' => $gceCredentials,
];
$storage = new StorageClient($config);
# Make an authenticated API request (listing storage buckets)
foreach ($storage->buckets() as $bucket) {
printf('Bucket: %s' . PHP_EOL, $bucket->name());
}
}
PHP Fatal error: Uncaught Error: Class 'GCECredentials' not found in /var/www/html/google_sheets/t2.php:5
More question:
Will this code create a json file as credential for me to access google sheets?
$gceCredentials = new GCECredentials();
Or where can i find the service account key??
Please tell me what should i do, thanks a lot.
I'm trying to authenticate the SpeechClient using 'keyFilePath' and 'projectId' parameters like so:
$speech = new SpeechClient([
'projectId' => 'actualProjectId,
'keyFilePath' => $key_path,
]);
If I use Google\Cloud\Speech\SpeechClient - Everything works fine, but if I use Google\Cloud\Speech\V1\SpeechClient I end up with an error: Could not construct ApplicationDefaultCredentials
I've read Google docs for Setting Up Authentication but still don't understand what am I doing wrong.
I need the V1 (in fact V1p1beta1) for additional features that are not available using the old SpeechClient.
Any ideas?
P.S. Using Laravel as the back-end.
For clients within the V1, etc. namespaces, pass the keyFilePath as credentials.
new SpeechClient([
'credentials' => $key_path
]);
I created a PHP script to retrieve some datas from my Google Cloud Platform account. Below is how I did :
<?php
require __DIR__ . '/vendor/autoload.php';
use Google\Cloud\BigQuery\BigQueryClient;
putenv('GOOGLE_APPLICATION_CREDENTIALS=key.json');
$projectId = 'xxxxx';
$datasetId = 'xxxxxx';
$table = 'xxxxx';
$bigQuery = new BigQueryClient([
'projectId' => $projectId
]);
// etc...
Everything works fine on my local computer (WAMP) but when I migrate my script to my company production environment, there is a problem :
Fatal error: Uncaught exception
'Google\Cloud\Core\Exception\ServiceException' with message 'cURL
error 6: Couldn't resolve host 'www.googleapis.com'
In fact I was excepting this message because every time I use Curl, I need to set our company proxy info :
<?php
curl_setopt($curl, CURLOPT_HTTPPROXYTUNNEL, true);
curl_setopt($curl, CURLOPT_PROXY, 'xxx.xxx.xxx.xxx');
By the way, i'm 100% sure that googleapis.com is white-listed by our proxies... but how to do it with the BigQueryClient ? I searched in the official documentation, no way to find how to use a proxy.
I would try passing blindly these constructions to one of the connection builder classes, hopefully one picks this up
'restOptions' => [
'proxy', 'xxx.xxx.xxx.xxx'
]
on the other hand if you have a trace log you could see if Guzzle or something else is used. Consider opening an issue tracking at: https://github.com/GoogleCloudPlatform/google-cloud-php/issues
Bigquery uses www.googleapis.com as its endpoint.
The command line instructions have global flags to specify an address, password, port and user name regarding a proxy usage, however, for client libraries you'll need to verify the access with your infrastructure team.
Use php's putenv() to set the proxy before using BigQueryClient.
putenv('HTTPS_PROXY=192.168.1.1:8080');
use Google\Cloud\BigQuery\BigQueryClient;
use GuzzleHttp\Client;
use Psr\Http\Message\RequestInterface;
$guzzleClient = new Client();
$config = [
'projectId' => 'xxx',
'keyFilePath' => 'key_path',
'restOptions' => [
'proxy' => 'xxx.xxx.xxx.xxx:xx'
],
'authHttpHandler' => function (RequestInterface $request, array $options = []) use ($guzzleClient) {
return $guzzleClient->send(
$request,
$options + [
'proxy' => 'xxx.xxx.xxx.xxx:xx'
]
);
}
];
$bigQueryClient = new BigQueryClient($config);
It works for me with php library google/cloud-bigquery version 1.8.0
I use AWS Services regularly and have my PHP SDK automatically retrieve credentials from my ec2 instance when I connect with Amazon.
I now have a library that I want to use which also requires my AWS secret key and access key to be included when I instantiate the class.
How can I retrieve the current access token and secret key through the AWS PHP SDK so I don't hard code keys into my application?
Where are you storing your AWS Credentials? In a credentials file or IAM Role?
[EDIT after the OP provided specific use case details]
From the link that you provided modify the example to look like this. Note: I have not tested the code, but this will be close:
// Require Composer's autoloader
require_once __DIR__ . "/vendor/autoload.php";
use Aws\Credentials\Credentials
use Aws\Credentials\CredentialProvider;
use Aws\Exception\CredentialsException;
use EddTurtle\DirectUpload\Signature;
// Use the default credential provider
$provider = CredentialProvider::defaultProvider();
$credentials = $provider()->wait();
$upload = new Signature(
$credentials->getAccessKeyId(),
$credentials->getSecretKey(),
"YOUR_S3_BUCKET",
"eu-west-1"
);
[END EDIT]
The simplest answer if you are using a credentials file is to open ~/.aws/credentials in a text editor and extract them. Otherwise follow the details below.
See the bottom for the actual answer on how to extract your access key once you have them loaded.
The following example will create a DynamoDB client using credentials stored in ~/.aws/credentials (normally created by the AWS CLI) from the profile named 'project1':
$client = new DynamoDbClient([
'profile' => 'project1',
'region' => 'us-west-2',
'version' => 'latest'
]);
However, usually you will want the SDK to locate your credentials automatically. The AWS SDK will search for your credentials in the following order (not all cases included):
Environment Variables (AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY, etc.)
In the default profile section of ~/.aws/credentials
EC2 IAM Role
Normally just use this example and let the SDK find the credentials for you:
use Aws\Credentials\CredentialProvider;
use Aws\S3\S3Client;
// Use the default credential provider
$provider = CredentialProvider::defaultProvider();
// Pass the provider to the client
$client = new S3Client([
'region' => 'us-west-2',
'version' => '2006-03-01',
'credentials' => $provider
]);
The SDK has a number of credential providers so that you can control exactly where your credentials are coming from.
PHP Class CredentialProvider
One item is that you mention Access Token. This means that you are using STS Assume Role type of access. The PHP SDK supports this also. Just dig into the documentation for STS:
PHP STS Client
Once you have loaded your credentials into a provider you can use the class Credentials to extract the three components (AccessKeyId, AcessKeySecret, SecurityToken):
PHP Class Credentials
im new in googleads library and im facing a problem
i added the google adswords api library to a new laravel 5.3 .
i make a call and retrieve data using the ini file but when i try to use the access on behalf of your client in
this wiki of the library
but it not working the final part i didnt understand
4. You can now use the OAuth2 object to make calls using the client library.
use Google\AdsApi\AdWords\AdWordsServices;
use Google\AdsApi\AdWords\AdWordsSessionBuilder;
use Google\AdsApi\Common\OAuth2TokenBuilder;
$session = (new AdWordsSessionBuilder())
->fromFile()
->withOAuth2Credential($oauth2)
->build();
$adWordsServices = new AdWordsServices();
$campaignService =
$adWordsServices->get($session, 'CampaignService', 'v201603', 'cm');
// Make calls using $campaignService.
when i try the code in the examples they given it give me error Undefined variable: oauth2
i try to put it from the connection file in session and retrieve it in the example file but not worked
one more question :
where i put the ClientCustomerId in on behalf of your client ??
Thanks
First of all, you are missing a small step. you need to OAuth2 instance as indicated in the tutorial like,
session_start();
$oauth2 = new OAuth2([
'authorizationUri' => 'https://accounts.google.com/o/oauth2/v2/auth',
'tokenCredentialUri' => 'https://www.googleapis.com/oauth2/v4/token',
'redirectUri' => '****',
'clientId' => '****',
'clientSecret' => '****',
'scope' => '****'
]);
ClientCustomerId should be placed in adsapi_php.ini file which you can find on Github.
after a while i discover the answer to my questions :
first :
when i try the code in the examples they given it give me error Undefined variable: oauth2
i try to put it from the connection file in session and retrieve it in
the example file but not worked
the answer of this question is in the wiki they make the code in one file. so if you want to put the code work put it in one file . and if you want to put it in other file or use one oauth2 for several files you need just to pass it
via route request
or via middleware
(laravel 5.* $request->attributes->add(['the_name_you_want' => $client]);
and you can retrieve it via this code $client = \Request::get('the_name_you_want') )
for the second question :
where i put the ClientCustomerId in on behalf of your client ??
this is easy to find just put it in the selector part like this
$session = (new AdWordsSessionBuilder())
->fromFile()
->withClientCustomerId('xxx-xxx-xxxx') //change it to what you want
->withOAuth2Credential($client)
->build();
and this is the final of my questions
thanks for helping for who's try to ;)