How can I authenticate Google Cloud Translate from PHP? - php

I'm trying to use the google/cloud-translate library (v ^1.5) in Laravel (v ^6.0).
In GoogleController.php:
public function translate(Request $request) {
$request->validate([
'source' => 'required|string|min:2|max:5',
'target' => 'required|string|min:2|max:5',
'q' => 'required|string',
]);
$translate = new TranslateClient([
'keyFile' => base_path(config('services.google.json_path')),
'projectId' => config('services.google.project_id'),
'suppressKeyFileNotice' => true,
]);
// Translate text from english to french.
$result = $translate->translate($request->q, [
'target' => explode($request->target, '-')[0],
'source' => explode($request->source, '-')[0],
]);
return $result;
}
But calling the route in Postman gives me the error:
Argument 2 passed to Google\Auth\CredentialsLoader::makeCredentials() must be of the type array, string given, called in /[...]/vendor/google/cloud-core/src/RequestWrapperTrait.php on line 155
I've checked that the projectId and the path to the keyFile is correct. Can anyone shed some light on how to get past this error?

You're specifying the path to the key file, so you should use the keyFilePath parameter instead.
Try this:
$translate = new TranslateClient([
'keyFilePath' => base_path(config('services.google.json_path')),
...
]);
From the TranslateClient.__construct docs:
keyFile: The contents of the service account credentials .json file retrieved from the Google Developer's Console. Ex: json_decode(file_get_contents($path), true).
keyFilePath: The full path to your service account credentials .json file retrieved from the Google Developers Console.

Related

Facebook Webhook Url verification is returning with additional string on botman studio

I am trying to verify my webhook url, however I am getting this error
Here is my code:
$config = [
'facebook' => [
'token' => 'page-token',
'app_secret' => 'app-secret',
'verification' => env('VERIFY_TOKEN')
]
];
DriverManager::loadDriver(FacebookDriver::class);
$botman = BotManFactory::create($config);
$botman->listen();
As noticed aboved I am getting additonal string added on the hub_challenge request. How will I able to solved this problem?

Credentials must be an instance of Aws\\Credentials\\CredentialsInterface error on PHP SDK with SQS

I am trying to delete a message from SQS in AWS using the php SDK. I have the following configuration.
$sqsClient = new SqsClient([
'version' => '2012-11-05',
'region' => 'us-east-1',
'credentials' => [
'key' => KEY,
'secret' => SECRET
]
]);
Then I am trying to delete like the following :
$sqsClient->deleteMessage([
'QueueUrl' => quque-url
'ReceiptHandle' => handle
]);
I am getting the following error on initialization :
Credentials must be an instance of Aws\\Credentials\\CredentialsInterface, an associative array that contains \"key\", \"secret\", and an optional \"token\" key-value pairs, a credentials provider function, or false."
The credentials I am using is correct. Before I was not passing the config and then also the same error was appearing. How this can be fixed ?
I found this code example and I think your code is correct.
How about create instance of Credentials?
You can use these code for initiating instance.
$credentials = new Aws\Credentials\Credentials(KEY, SECRET);
$sqsClient = new SqsClient([
'version' => '2012-11-05',
'region' => 'us-east-1',
'credentials' => $credentials
]);
Check this document.
And for security reason, I recommend that you'd better use credentials file or set environment variable.
I hope all is well.

Adobe EchoSign API v5 PHP sample

How can I send someone a document for signing using Adobe EchoSign API v5? For API version 5, I could only find an example using Java. But I need it in PHP. If I use any earlier version it requires an "API key", but in the current version of API, I can't find my API key. I can't even find any sample code in PHP.
There's no longer an API key in the current version as it now uses "Access Token". This is what I did on my PHP application.
Store the Application ID and Client Secret and use it for requesting access token.
If request is successful, store both access token and refresh token and the timestamp in your database.
Access token will expire in 1 hour so you have to check if it already expires.
If access token has expired. Request for refresh token and it should return a new access token.
Replace the access token you stored with the new one you receive and regain access to the API.
I took me a while but I got it working, I hope it helps others, after installing with composer (https://github.com/kevinem/adobe-sign-php/) yo need to create your sandbox page, for me was a index.php in /vendor/kevinem/adobe-sign-php/ then you need to include 2 classes AdobeSign and Psr7 in my case, because I need to upload 2 documments.
require "../../../vendor/autoload.php";
use KevinEm\AdobeSign\AdobeSign;
use GuzzleHttp\Psr7;
this is how I configure my $provider, the call backurl must be the same as the one in the backend of adobe and I changed manually the data center in AdobeSign.php
$provider = new KevinEm\OAuth2\Client\AdobeSign([
'clientId' => 'YOUR_APP_ID',
'clientSecret' => 'YOUR_APP_SECRET',
'redirectUri' => 'YOUR_BACK_URL',
'dataCenter' => 'secure.na2',
'scope' => ['user_login:self',
'agreement_write:self',
'widget_write:self',
'library_write:self',
'agreement_send:self'
]
]);
Then u need to create an array with the URL of all your .pdf documents. my is call $archivos.
$accessToken = $adobeSign->getAccessToken($_GET['code']);
$adobeSign->setAccessToken($accessToken->getToken());
$todosDoc = array();
foreach ($archivos as $doc) {
$file_path = __DIR__.$doc;
$file_stream = Psr7\FnStream::decorate(Psr7\stream_for(file_get_contents($file_path)), [
'getMetadata' => function() use ($file_path) {
return $file_path;
}
]);
$multipart_stream = new Psr7\MultipartStream([
[
'name' => 'File',
'contents' => $file_stream
]
]);
$transient_document = $adobeSign->uploadTransientDocument($multipart_stream);
array_push($todosDoc, $transient_document);
}
$adobeSign->createAgreement([
'documentCreationInfo' => [
'fileInfos' => $todosDoc,
'name' => 'My Document Test',
'signatureType' => 'ESIGN',
'recipientSetInfos' => [
'recipientSetMemberInfos' => [
'email' => 'orlando#yoursigner.com'
],
'recipientSetRole' => [
'SIGNER'
]
],
'mergeFieldInfo' => [
[
'fieldName' => 'Name',
'defaultValue' => 'John Doe'
]
],
'signatureFlow' => 'SENDER_SIGNATURE_NOT_REQUIRED'
]
]);
Adobe Sign has changed its authentication process and instead of using "API key", it now uses "Access-Token" which is more secure.
You can use any PHP based third part rest API client library for the Adobe Sign API calls until Adobe Sign itself provides the SDK for PHP.
Adobe eSign is now using oAuth for authentication, and there are a lot of different libraries for different languag

AWS Lambda PHP Create Function with Zip

I'm trying to create a PHP script that creates a function from some code that i zip up on our server. I uploaded the file manually to lambda, and it works fine. But when i try to use the aws sdk to create the function, it fails with an error message. Anyone got any clue?
Code:
private function createLambdaFunction() {
$result = $this->lambdaConn->createFunction(array(
'FunctionName' => $this->lambdaFunctionName,
'Runtime' => $this->runtime,
'Role' => $this->role,
'Handler' => $this->lambdaFunctionName.".".$this->handler,
'Description' => $this->description,
'Timeout' => $this->timeout,
'MemorySize' => $this->memorySize,
'Code' => array(
'ZipFile' => 'fileb://test.zip'
)
));
Error:
PHP Fatal error: Uncaught Aws\Lambda\Exception\LambdaException: AWS
Error Code: InvalidParameterValueException,
Status Code: 400, AWS Request ID: asdf, AWS Error Type: user,
AWS Error Message: Could not unzip uploaded file. Please check
your file, then try to upload again., User-Agent:
aws-sdk-php2/2.8.10 Guzzle/3.9.3 curl/7.35.0 PHP/5.5.9-1ubuntu4.9
I can't seem to find a good example on google, and the documentation is...less than ideal. I created the zip file with php, so I've tried passing that var, the full path to the file, relative path to file, etc. Finally learned you have to use fileb:// preface, but that didn't end up fixing anything.
Okay, I'm not sure why this is the case, but you need to base64 encode your zip file like:
$result = $this->lambdaConn->createFunction(array(
'FunctionName' => $this->lambdaFunctionName,
'Runtime' => $this->runtime,
'Role' => $this->role,
'Handler' => $this->lambdaFunctionName . "." . $this->handler,
'Description' => $this->description,
'Timeout' => $this->timeout,
'MemorySize' => $this->memorySize,
'Code' => array(
'ZipFile' => 'fileb://'.base64_encode(file_get_contents('test.zip'))
)
));
I'm not sure why this is required, as accourding to the doumentation and a post by an AWS employee, you dont have to have base64 encoding for create function. They must have mixed up something or another.

How do i update object using php-sdk? (The parameter object is required)

$response = $facebook->api(
'me/objects/namespace:result',
'POST',
array(
'app_id' => app_id,
'type' => "namespace:result",
'url' => "http://samples.ogp.me/370740823026684",
'title' => "Sample Result",
'image' => "https://fbstatic-a.akamaihd.net/images/devsite/attachment_blank.png",
'description' => ""
)
);
I get the following error.
The parameter object is required
I don't know where to add the parameter object.
I have been facing the same issue for the past couple of days, in the end I stopped trying to use the Facebook PHP SDK to send the request and resorted to using just sending a HTTP Request.
I am creating an object rather than updating one, but the implementation shouldn't differ much from your needs. I was getting the same error (The parameter object is required) and the below implementation resolved that issue.
I used the vinelab HTTP client composer package and built a request like this:
$request = [
'url' => 'https://graph.facebook.com/me/objects/namespace:object',
'params' => [
'access_token' => $fbAccessToken,
'method' => 'POST',
'object' => json_encode([
'fb:app_id' => 1234567890,
'og:url' => 'http://samples.ogp.me/1234567890',
'og:title' => 'Sample Object',
'og:image' => 'https://fbstatic-a.akamaihd.net/images/devsite/attachment_blank.png',
'og:description' => 'Sample Object'
])
]
];
// I'm using Laravel so this bit might look different for you (check the vine lab docs if you use that specific HTTP client)
$response = HttpClient::post($request);
// raw content
$response->content();
// json
$response->json();
As I said, I used the vinelab HTTP package in my implementation, you should be able to use any similar package or directly use curl in PHP instead.

Categories