How to authenticate SpeechClient V1 using keyfile (PHP / Laravel) - php

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
]);

Related

Get AWS IAM credentials from PHP SDK

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

Authentication issue in Google Cloud Client Library

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.

How to use google ads api access on behalf your client

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 ;)

Can't pass my credentials to AWS PHP SDK

I installed AWS PHP SDK and am trying to use SES. My problem is that it's (apparently) trying to read ~/.aws/credentials no matter what I do. I currently have this code:
$S3_AK = getenv('S3_AK');
$S3_PK = getenv('S3_PK');
$profile = 'default';
$path = '/home/franco/public/site/default.ini';
$provider = CredentialProvider::ini($profile, $path);
$provider = CredentialProvider::memoize($provider);
$client = SesClient::factory(array(
'profile' => 'default',
'region' => 'us-east-1',
'version' => "2010-12-01",
'credentials' => [
'key' => $S3_AK,
'secret' => $S3_PK,
]
));
And am still getting "Cannot read credentials from ~/.aws/credentials" error (after quite a while).
I tried 'credentials' => $provider of course, that was the idea, but as it wasn't working I reverted to hardcoded credentials. I've dumped $S3_AK and $S3_PK and they're fine, I'm actually using them correctly for S3, but there I have Zend's wrapper. I've tried ~/.aws/credentials (no ".ini") to the same result. Both files having 777 permissions.
Curious information: I had to set memory limit to -1 so it would be able to var_dump the exception. The html to the exception is around 200mb.
I'd prefer to use the environment variables, all though the credentials file is fine. I just don't understand why it appears to be trying to read the file even though I've hardcoded the credentials.
EDIT: So a friend showed me this, I removed the profile and also modified the try/catch and noticed the client seems to be created properly, and the error comes from trying to actually send an email.
The trick is just remove 'profile' => 'default' from the factory params, if this is defined we can't use a custom credentials file or environment variables. Is not documented but just works.
I'm using Sns and Sdk v3.
<?php
use Aws\Credentials\CredentialProvider;
$profile = 'sns-reminders';
$path = '../private/credentials';
$provider = CredentialProvider::ini($profile, $path);
$provider = CredentialProvider::memoize($provider);
$sdk = new Aws\Sdk(['credentials' => $provider]);
$sns = $sdk->createSns([
// 'profile' => $profile,
'region' => 'us-east-1',
'version' => 'latest',
]);
This solution will probably only work if you're using version 3 of the SDK. I use something similar to this:
$provider = CredentialsProvider::memoize(CredentialsProvider::ini($profile, $path));
$client = new SesClient([
'version' => 'latest',
'region' => 'us-east-1',
'credentials' => $provider]);
I use this for S3Client, DynamoDbClient, and a few other clients, so I am assuming that the SesClient constructor supports the same arguments.
OK, I managed to fix it.
I couldn't read the credentials file but it wasn't exactly my idea.
What was happening was that the actual client was being created successfully, but the try/catch also had the sendEmail included. This was what was failing.
About creating the client with explicit credentials: If you specify region, it will try and read a credentials file.
About the SendEmail, this is the syntax that worked for me, I'd found another one also in the AWS docs site, and that one failed. It must've been for an older SDK.

create new user IAM with aws php sdk and limit access of one bucket

i am using aws php sdk for creating bucket in S3
i want to create new user IAM using aws php sdk.. .and then i want to save userkey and acceskey. ..
I got the tutorial for limit the access to user,but not get any for creating new user.
is there any way to create new user?
Install AWS SDK - http://docs.aws.amazon.com/aws-sdk-php/guide/latest/installation.html
Create an Iam client however you like by supplying your AWS credentials. The easiest example to demonstrate is putting the credentials in the PHP file directly - See http://docs.aws.amazon.com/aws-sdk-php/guide/latest/credentials.html
Then this example will create a user
<?php
require 'vendor/autoload.php';
$iamClient = \Aws\Iam\IamClient::factory(
[
'credentials' => [
'key' => 'YOUR_ACCESS_KEY',
'secret' => 'YOUR_SECRET_KEY'
]
]
);
$result = $iamClient->createUser(
[
// UserName is required
'UserName' => 'carlton',
// Optional
'Path' => '/packager/dev/'
]
);
4.Check http://docs.aws.amazon.com/aws-sdk-php/latest/class-Aws.Iam.IamClient.html for all of the methods available on the $iamClient variable we created. A good IDE will provide code completion on $iamClient so you can see available methods that way.

Categories