Can't create session client dialogflow - php

I'm trying to set up Dialogflow on a php backend. According to the documentation: https://googleapis.dev/nodejs/dialogflow/latest/v2beta1.SessionsClient.html
I should be able to pass as a parameter the path of the json key file I previously generated with the Google Cloud Console to access DialogFlow's API.
$path = dirname(__DIR__, 2) . '\_________-_______-____________.json';
$config = [
'keyFilename' => $path,
];
$sessionsClient = new SessionsClient($config);
I keep getting this error: Could not construct ApplicationDefaultCredentials
I double checked the path using the file_get_contents method, it's valid.
Any idea ?

Something is wrong with the doc.
Using the key credentials instead of keyFilename did the work.

Related

Issue when create a user on Azure B2C using Microsoft Graph API

Issue
I'm trying to build a little script PHP to integrate with the Microsoft Graph API.
I have an admin account in Azure, and have set up a new app through the portal. I've downloaded and installed the PHP SDK, and have managed to set everything up so that I can get a user successfully.
However, POSTing a new user returns me a 400 bad request:
$newUser = new User();
$newUser->setAccountEnabled(true);
$newUser->setGivenName('name');
$newUser->setSurname('surname');
$newUser->setUserPrincipalName('email#dot.com');
$password = new PasswordProfile();
$password->setPassword("pwd");
$password->setForceChangePasswordNextSignIn(false);
$newUser->setPasswordProfile($password);
$identities = new ObjectIdentity();
$identities->setSignInType("emailAddress");
$identities->setIssuer("a tenantId");
$identities->setIssuerAssignedId("email#dot.com");
$newUser->setIdentities($identities);
$user = $this->graph->createRequest('POST', '/users')
->attachBody($newUser)
->setReturnType(Model\User::class)
->execute();
Returns:
{"error":{"code":"BadRequest","message":"Property identities in payload has a value that does not match schema.","innerError":{"date":"2021-11-15T15:26:24","request-id":"an id","client-request-id":"an id"}}}
How can I solve this issue?
Thanks for any help.
Update
I have found a solution for this problem (obviously, is temporary).
The section "identities" must be set as an array (in JSON) like in this example
"identities": [
{
"signInType": "emailAddress",
"issuer": "contoso.onmicrosoft.com",
"issuerAssignedId": "jsmith#yahoo.com"
}
]
Probably, the PHP code above not set the section "identities" as an array.
Now, I send directly a JSON with the section "identities" as an array but I would like to use the PHP code above.
How can I transform the PHP code above to pass "identities" as an array like in the JSON example?
Thanks again.
Try creating an array and adding the identities on there like. This will add the identities property as an array of Model\ObjectIdentity()
...
$identities = [];
$identity = new Model\ObjectIdentity();
$identity->setSignInType("type");
$identity->setIssuer("issuer");
$identity->setIssuerAssignedId("issure-assigned-id");
array_push($identities, $identity);
$newUser->setIdentities($identities);
...

Python: Invalid AWS Access key error

I am using the following example code to fetch details. And it's giving error:
amazonproduct.errors.InvalidClientTokenId: InvalidClientTokenId: The
AWS Access Key Id you provided does not exist in our records.
The same is working while I used in PHP. I generated the key going to IAM Console.
access_key = '***'
secret_key = '***'
associate_tag = 'ptab-20'
api = API(locale='us')
result = api.item_lookup('B006H3MIV8')
for item in result.Items.Item:
print '%s (%s)' % (item.ItemAttributes.Title, item.ASIN)
Somehow python-amazon-product-api is not considering the config file i.e. ~/.amazon-product-api. Alternatively you can specify your credentials directly when instantiating API object by
api = API(AWS_KEY, SECRET_KEY, "us")

Download url not working in Google Drive API PHP

I am having issue with the Google Drive API, i was able to fetch the files using API But i can't download via this link. I guess, must some auth, but i have used refresh tokens to authenticate.Please see below for my code
$this->load->library('google-api-php-client/src/Google_Client');
include APPPATH . '/libraries/google-api-php-client/src/contrib/Google_DriveService.php';
// Library Files Configuration to get access token and Refresh Token
$client = new Google_Client();
$client->setAccessType('offline'); // default: offline
$client->setApplicationName('xxx'); //name of the application
$client->setClientId('yyyy'); //insert your client id
$client->setClientSecret('zzz'); //insert your client secret
$client->setScopes(array('https://www.googleapis.com/auth/drive'));
$service = new Google_DriveService($client);
$client->refreshToken($drive_data->refreshtoken);
$client->getAccessToken();
$parameters = array();
$files = $service->files->listFiles($parameters);
foreach ($files['items'] as $key => $items)
{
Download
}
Anybody knows how to get the download url with authentication?
This is having the answer:
(Java) Download URL not working
There seem to be some changes on v2 of GDrive, instead of using "downloadUrl" you may have to use "webContentLink" for getting the download link
To get downloadUrls, you need to get the metadata of a file. You can do so by using the get method. The method will return a File Resource representation. In this resource, there is a downloadUrl property. If you're able to access the files and get the URL already, then there should be no problem with your authentication setup. There could be permission issues where you may not have access to certain drive files, but if you receive no error for it, you should be fine there too. I am not particularly familiar with PHP, but perhaps you are not downloading it correctly? Here it seems to be done differently.
I also suggest that you check out the Drive PHP Quickstart App to use as a reference.
I have bumped into the same problem today and just found a solution for my case. I hope that this helps the one or another confused PHP coder out there who also does not get a downloadUrl. I assume that you are working with the examples of the v2 API, as seen on https://developers.google.com/drive/v2/reference.
First, I have altered the head to not only access the metadata but get full access (mind the DRIVE constant):
<?php
require 'vendor/autoload.php';
const DRIVE = "https://www.googleapis.com/auth/drive";
define('APPLICATION_NAME', 'MAGOS poller');
define('CREDENTIALS_PATH', 'credentials.json');
define('CLIENT_SECRET_PATH', 'client_secret.json');
define('SCOPES', implode(' ', array(Google_Service_Drive::DRIVE)));
Then I have deleted my credentials file (credentials.json) and reran the script so it authenticated once more against gDrive and recreated the credentials file. After that
$downloadUrl = $file->getDownloadUrl();
finally worked like a charm.

Using Google's API through laravel/hybridauth

I want to create an app that uses Google+ APIs through hybridauth.
I'm using atticmedia/anvard version of hybridauth, that is already configured with Google's clientID and secretKey that have been generated through Google Developer Console (I have inserted these info inside the hybridauth.php file inside the config folder of laravel). I have setted the scope too (as Google suggest).
"scope" => "https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.email",
I do the following in a laravel route:
if (!$hybridauth->isConnectedWith('Google')) {
$adapter = $hybridauth->authenticate('Google');
}
else {
$adapter = $hybridauth->getAdapter('Google');
}
$profile = $adapter->getUserProfile();
Till now, everything goes well. The profile is correctely printed using the var_dump() function. So I can assume I am logged in. Now I want to make a call to Google APIs (for example this). In the same laravel route, after printing the user's profile, i do the following:
$answer= $adapter->api()->api('/people', 'get', array(
'query' => 'Google'
));
As shown in this page, I can use the api() method to do the call. But the only result I can print is "NULL". I suspect that somehow the request is not correct, but I tryed almost anything, and I have not found yet a "real" example of Google API in conjuction with laravel/hybridauth.
When calling $adapter->api in hybridauth to access Google APIs, you must use the full HTTP URL request.
$answer= $adapter->api()->api('https://www.googleapis.com/plus/v1/people/me');
For other services, such as Facebook, you don't need to
$answer= $adapter->api()->api('/me');
I'm using Laravel 4.2.11 and hybridauth dev-master
Reference: http://hybridauth.sourceforge.net/userguide/tuts/advanced-access-google-api.html

Not able to upload object to google cloud storage using google API

I am using the following link for using the Google Cloud Storage :
Google CLoud
I want to upload an object using insert function given in the above API.I am using PHP. The code i am using is as follows :
$StorageService = new Google_StorageService($client);
$objects = $StorageService->objects;
$gso = new Google_StorageObject();
$gso->setName('myobj');
$postbody = array(file_get_contents('buc.jpg'));
$resp = $objects->insert('mybucket', $gso, $postbody);
But i m getting Error as :
Fatal error: Uncaught exception 'Google_ServiceException' with message 'Error calling POST https://www.googleapis.com/storage/v1beta1/b/mybucket/o: (400) Required' in /home/www/public_html/abc/google-api-php-client/src/io/Google_REST.php on line 66
What am i doing wrong please help me out..
There is some problem with the way i am sending the parameters. Please if anyone knows help me..
There is an example here :
Example
But that is in Java Please help me to do the same thing in PHP. Please help me out.
I figured out the answer. The code to be used is as follows :
$objects = $StorageService->objects;
$postbody = array('data' => file_get_contents('buc.jpg'));
$gso = new Google_StorageObject();
$gso->setName('mybuc');
$resp = $objects->insert('mybucket', $gso ,$postbody);
print_r($resp);
and then it works ..
I was not setting the parameter "data" in an array to be passed as a third parameter of the function. I Found out and it worked.
Buckets share a global namespace. You have to create your bucket before storing objects in it, and "mybucket" is not an available name.
If you go to the GCS browser at https://storage.cloud.google.com/, you can create your bucket there and then reference the existing bucket in your PHP code.
I had to add uploadType to get it to work as mentioned here: https://cloud.google.com/storage/docs/json_api/v1/objects/insert
$postbody = array('uploadType' => 'media', 'data' => file_get_contents('buc.jpg'));
Where is the data parameter documented?

Categories