Zend Framework 2 ZendOAuth with Google - php

I'm semi-familiar with the ZendFramework/ZendOAuth library found here https://github.com/zendframework/ZendOAuth
I am able to use it no problem with Twitter, but I can't figure out how to get it to work with Google OAuth 2.0. The code I'm trying to use is as follows:
$config = array(
'callbackUrl' => 'https://www.example.com/callback',
'siteUrl' => 'https://accounts.google.com/o/oauth2',
'userAuthorizationUrl' => 'https://accounts.google.com/o/oauth2/auth',
'requestTokenUrl' => 'https://accounts.google.com/o/oauth2/token',
'consumerKey' => 'MY-CONSUMER-KEY-HERE',
'consumerSecret' => 'MY-SECRET-KEY-HERE',
'version' => '2.0',
);
$scopes = array(
'https://www.googleapis.com/oauth2/v2/userinfo'
);
$consumer = new Consumer($config);
$token = $consumer->getRequestToken(array('scope' => implode('', $scopes), 'response_type' => 'code', 'redirect_uri' => 'https://www.example.com/callback'));
$consumer->redirect();
According to the documentation for ZendOAuth, an array in the getRequestToken function gets treated as custom OAuth parameters. However, when I run this code I get redirected to a Google page that says:
Required parameter is missing: response_type
The URL in the browser shows the following:
https://accounts.google.com/o/oauth2/auth?oauth_token=&oauth_callback=https%3A%2F%2Fwww.example.com%2Fcallback
I've tried throwing debug statements around in the ZendOAuth library, but I can't seem to be able to find the problem.
If anyone has a solution to this, it would be much appreciated.
Thanks,

I believe the ZendOAuth package only supports OAuth 1, so for OAuth 2 you'll need to find a different library. I've used https://github.com/php-loep/oauth2-client with ZF2 applications so you could start there.

Related

Facebook Marketing API PHP SDK - "Filtering field delivery_info is invalid"

I'm attempting to make a PHP-application that generates Facebook ad reports for a company, using the Facebook PHP Ads SDK. I'm following the Marketing API QuickStart that essentially generates code for you. I have all access I need to reach the company's ad account ID.
(I'm not showing the token- and ID-variables, unless absolutely necessary. If so, tell me).
require __DIR__ . '/vendor/autoload.php';
use FacebookAds\Object\AdAccount;
use FacebookAds\Object\AdsInsights;
use FacebookAds\Api;
use FacebookAds\Logger\CurlLogger;
$access_token;
$ad_account_id;
$app_secret;
$app_id;
$api = Api::init($app_id, $app_secret, $access_token);
$api->setLogger(new CurlLogger());
$fields = array(
'frequency',
'actions:link_click',
'call_to_action_clicks',
'actions:tab_view',
);
$params = array(
'level' => 'adset',
'filtering' => array(array('field' => 'delivery_info','operator' => 'IN','value' => array('inactive','active','limited','archived','permanently_deleted','completed','recently_completed','not_delivering','not_published','rejected','recently_rejected','rejected','pending_review','scheduled'))),
'breakdowns' => array('place_page_id'),
'time_range' => array('since' => '2017-09-20','until' => '2017-10-20'),
);
echo json_encode((new AdAccount($ad_account_id))->getInsights(
$fields,
$params
)->getResponse()->getContent(), JSON_PRETTY_PRINT);
However, when running the code I get the error message:
Fatal error: Uncaught exception
'FacebookAds\Http\Exception\AuthorizationException' with message
'(#100) Filtering field delivery_info is invalid.
I try switching around in $fields and $params with parameters found in the documentation, but it switches the blame on other ones as I do, even claiming that some of them don't even exist as alternatives.
I've checked several times in the documentation, it should work.
Does the problem lie elsewhere, is it the wrong kind of ad account ID?
Any help would be very much appreciated.
Try adset.delivery_info instead of just delivery_info. That should work. I learned the hard way, seems like you need to append the object level separated by period for filtering options.

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.

Getting object reference not set to an instance of an object PHP Soap Client

I am trying to call a courier api with specified method. I am able to connect with the api using soapclient but getting following error:
Object reference not set to an instance of an object
I am using following code and data
$proxy = new SoapClient($my_api_url);
$params = array(
"UserName" => '****',
"Password" => '****',
"OrderNumber" => '41111',
"ClientName" => 'My Name',
"ContactNumber1" => '123456789',
"EmailAddress" => 'testapi#rohitdhiman.in',
"ShippingAddress1" => 'site 15'
);
$result = $proxy->BayOneAddOrder($params);
print_r($result);
If it works using SOAP UI then you can try using a PHP tool like https://providr.io as it will give you the exact PHP request using OOP approach.
If you do not want to use the online tool, then you can generate your own PHP package from your WSDL using PackageGenerator so you'll send request easily still using OOP approach.

AWS - You are not authorized to perform this operation on accessing describeInstanceStatus from ec2 client object

I have created an ec2 client using the method mentioned in the AWS docs. I am using the aws.phar file for the SDK. The ec2 client is created properly because when I var_dump the client, it returns the Ec2Client object. But when I attempt to access the describeInstanceStatus from the ec2 client it throws a You are not authorized to perform this operation. exception. This is my code.
use Aws\Ec2\Ec2Client;
require 'aws.phar';
$ec2Client = Ec2Client::factory(array(
'key' => '<aws access key>',
'secret' => '<aws secret key>',
'region' => 'us-east-1'
));
try{
$ec2Client->describeInstanceStatus(array(
'DryRun' => false,
'InstanceIds' => array('InstanceId'),
'Filters' => array(
array(
'Name' => 'availability-zone',
'Values' => array('us-east-1'),
),
),
'MaxResults' => 10,
'IncludeAllInstances' => false,
));}
catch(Exception $e){
echo $e->getMessage();
}
Please tell me where am I getting this wrong. I've tried googling it, looked in the AWS forums but to no result. Thank you.
The error is coming from the Access that you have been granted/denied via AWS IAM.
The user, whose access/secret keys you are using in the code, does not have privilege to describe instances. This privilege is configured in the IAM policy which is applied to this user.
There is nothing wrong with your code. You need to look into the IAM policy about what all privileges are granted/denied to this user.

Can you create a MySQL DB via cPanel API json call?

I am looking to automate the creation of a MySQL database via a json api call. To list dbs, I can just use something like:
https://example.com:2083/json-api/cpanel?user=username&cpanel_jsonapi_module=MysqlFE&cpanel_jsonapi_func=listdbs&cpanel_jsonapi_version=2
This is successful via HTTP Sockets and CURL. Is there any equivalent call for adddb?
https://example.com:2083/json-api/cpanel?user=username&cpanel_jsonapi_module=Mysql&cpanel_jsonapi_func=adddb&dbname=aa1&cpanel_jsonapi_version=1
This doesn't work. I get the following error:
“username_†is an invalid database name. It contains invalid characters.
Any ideas?
UPDATE:
I am running this via an HTTP Socket connection in CakePHP with the following code:
$query = 'cpanel_jsonapi_module=MysqlFE&cpanel_jsonapi_func=adddb&dbname=aa1&cpanel_jsonapi_version=2';
$request = array(
'auth' => array(
'user' => $queryData['conditions']['username'],
'pass' => $queryData['conditions']['password'],
'method' => 'Basic',
),
'uri' => array(
'scheme' => 'https',
'host' => $queryData['conditions']['host'],
'port' => '2083',
'user' => $queryData['conditions']['username'],
'path' => 'json-api/cpanel',
'query' => $query,
),
);
$result = json_decode($this->connection->request($request), true);
http://docs.cpanel.net/twiki/bin/view/ApiDocs/Api1/ApiMysql#Mysql::adddb
You've got dbname in the url twice but I don't know if that matters...
I'm not sure how the url API works, you could try ..._jsonapi_func=adddb(thedbname) but I have no clue if that would work.
Found the solution. In order to create / delete a DB through the cPanel API, it must be processed through API 1. The API access is a little bit different.
You must first get the theme your cPanel account is using (StatsBar:stat (theme):
https://example.com:2083/json-api/cpanel?user=USERNAME&cpanel_jsonapi_module=StatsBar&cpanel_jsonapi_func=stat&display=theme&cpanel_jsonapi_version=2
Then you plug that theme into another request:
https://example.com:2083/frontend/THEME/sql/addb.html?db=aa1
Of course, you must have the Authentication in the HTTP Socket (or CURL). Works like a charm!

Categories