How to use google ads api access on behalf your client - php

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

Related

How to create a credential (for google sheets) on GCE with php code?

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.

php auth0 api update app_data

I am trying to use auth0 api for php. I strated with this examples:
enter link description here
Every time when I start I got error that I'm calling non-defined class in dotenv-autoloader (Dotenv::load(__DIR__);). I avoid this and I wrote manually parameter. After that I can create, delete user, change user_metadata, but I can't change app_matadata. Always got same error: {"statusCode":401,"error":"Unauthorized","message":"Missing authentication"}
Can someone help me with this issue?
Thanks.
what error did you get with Dotenv?
3 things might happened:
it was not installed (which is weird since it is a dependency in the
composer.json file)
there was no .env file with the configuration
the .env file had the wrong format
Anyway, if you set the configuration manually is enough.
About the app_metadata how are you updating it?
this snippet should work:
require __DIR__ . '/vendor/autoload.php';
use Auth0\SDK\Auth0Api;
$token = "eyJhbGciO....eyJhdWQiOiI....1ZVDisdL...";
$domain = "account.auth0.com";
$user_id = 'auth0|123...';
$auth0Api = new Auth0Api($token, $domain);
$usersList = $auth0Api->users->update( $user_id, [ "app_metadata" => [ "some_attribute" => "some_value" ] ]);
var_dump($usersList);
One important thing, to update the app_metadata you need an api token with the update:users_app_metadata scope, you can't use the user token for it.

Setup google-api-php-client to comunicate with custom google app engine API endpoint

I'm trying to configure the google-api-php-client library in my project.
I've already created a custom google app engine project that consists in a cloud endpoint. The project is called 'set-core', the service is called 'vrp API', version 'v1' and the method vrp.vrp.getSolution().
Now in my PHP code i'm following this example:
https://developers.google.com/api-client-library/php/start/get_started#building-and-calling-a-service
The problem is that in this example there's no mention how to connect to any custom service, outside Google's ones.
My PHP code is:
$client = new Google_Client();
$client->setApplicationName("set-core");
$client->setDeveloperKey("AIzaSyByd8cRJNGYC4szFLbr3**************");
$client->isAppEngine(true);
$service = new Google_Service_Appengine_Service($client);
$results = $service->vrp->vrp.vrp.getSolution($stringVehicles, $stringServices, $stringDepot);
Unfortunately, on the last line, PHP warns me:
Notice: Trying to get property of non-object (I assume it's $service).
The problem is that I don't really know how to set up all the client's params and which Service type use.
You are going to want to create an authorized HTTP client and then request your API endpoint directly with it. The AppEngine service classes you're manipulating above are not meant for this use case. Something like this should work:
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$httpClient = $client->authorize();
$response = $httpClient->request('GET', 'https://myapp.appspot.com/vrp/getSolution');
The $httpClient class is an instance of GuzzleHttp\Client, but with your Google authentication already added to it. See the documentation for making a request with Guzzle.
I hope this helps!

How to setup a Facebook API session

I am exploring the Facebook API for a couple of days now, because I am trying to get leads from my Facebook page using Webhooks with Lead Ads.
After reading a bunch of articles and Facebook documentation I noticed that for a lot functions in the documentation you see a $session variable defined (for example here and here).
Because I want to get the leads from my Facebook page I started following this article, which explains how to get real time lead updates from Facebook.
The article starts with setting up a request for receiving the app subscriptions for your Facebook page. But I directly came into trouble with creating the session, because it looks like the FacebookSession class does not exists in the current Facebook SDK anymore.
How do I need to setup this session? Because I can not find anywhere in the SDK files the FacebookSession class, so I think in the mean time this is changed.
require_once __DIR__ . '/facebook-sdk-v5/autoload.php';
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
// I cannot find this class anywhere
$session = new FacebookSession('<APP_ACCESS_TOKEN>');
$request = new FacebookRequest(
$session,
'POST',
'/<APP_ID>/subscriptions',
array(
'object' => 'page',
'callback_url' => '<YOUR_CALLBACK_URL>',
'fields' => 'conversations',
'verify_token' => '<YOUR_VERIFY_TOKEN>',
)
);
$response = $request->execute();
I get an 500 server error with the following message:
Fatal error: Class 'Facebook\FacebookSession' not found in ...

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