Struggling to store token for AdSense API - google-api

I've successfully managed to connect to the AdSense API and run a report. However, it requires a log in each time I run it, so it won't run as a cron job.
I've found a few other questions related to this. Some advise a service account, while others point out that a service account does not work with AdSense. The proposed solution is to store a token on my server, but I've been struggling to get that to work. Here is my code so far (which works, but requires manual log in):
$scriptUri = "http://".$_SERVER["HTTP_HOST"].$_SERVER['PHP_SELF'];
$client = new Google_Client();
$client->addScope('https://www.googleapis.com/auth/adsense.readonly');
$client->setAccessType('offline');
$client->setApplicationName('My Application name');
$client->setClientId(' MY ID ');
$client->setClientSecret(' MY SECRET ');
$client->setRedirectUri($scriptUri);
$client->setDeveloperKey(' MY KEY '); // API key
$accountId = " MY ACCOUNT " ;
$adClientId = " MY CLIENT " ;
// $service implements the client interface, has to be set before auth call
$service = new Google_Service_AdSense($client);
if (isset($_GET['logout'])) { // logout: destroy token
unset($_SESSION['token']);
die('Logged out.');
}
if (isset($_GET['code'])) { // we received the positive auth callback, get the token and store it in session
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
}
if (isset($_SESSION['token'])) { // extract token from session and configure client
$token = $_SESSION['token'];
$client->setAccessToken($token);
}
if (!$client->getAccessToken()) { // auth call to google
$authUrl = $client->createAuthUrl();
header("Location: ".$authUrl);
die;
}
$startDate = '2015-11-01';
$endDate = 'today';
$optParams = array(
'metric' => array(
'EARNINGS'),
'dimension' => array('DATE'),
'sort' => '+DATE',
'filter' => array(
'CUSTOM_CHANNEL_NAME==Mega Seating Plan'
)
);
// Run report.
$report = $service->accounts_reports->generate($accountId, $startDate,
$endDate, $optParams);
if (isset($report) && isset($report['rows'])) {
// Get results.
foreach($report['rows'] as $row) {
$date = $row[0] ;
$earnings[$date] = $row[1] ;
}
} else {
print "No rows returned.\n";
}
Can anybody give me any pointers about how I can incorporate token storage into the above code, please?

Thank you to #jkns.co for the previous answer here which helped me to get it working.
Here's my final code:
$scriptUri = "I HAD TO PUT MY ABSOLUTE URL HERE, OTHERWISE THE CRON JOB WOULD LOOK IN THE WRONG PLACE" ;
$client = new Google_Client();
$client->addScope('https://www.googleapis.com/auth/adsense.readonly');
$client->setAccessType('offline');
$client->setApprovalPrompt ("force"); // This line had to be added to force the approval prompt and request a new token
$client->setApplicationName('My Application name');
$client->setClientId('BLAH');
$client->setClientSecret('BLAH');
$client->setRedirectUri($scriptUri);
$client->setDeveloperKey('BLAH'); // API key
$accountId = "BLAH" ;
$adClientId = "BLAH" ;
// $service implements the client interface, has to be set before auth call
$service = new Google_Service_AdSense($client);
if (isset($_GET['logout'])) { // logout: destroy token
unset($_SESSION['token']);
die('Logged out.');
}
if (isset($_GET['code'])) { // we received the positive auth callback, get the token and store it in session
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
// If it successfully authenticates, I request the refresh token
$refreshToken = $client->getRefreshToken();
storeRefreshToken($refreshToken) ; // This function stores the token in MySQL
}
else { // Otherwise it loads the refresh token from MySQL
$refreshToken = getRefreshToken() ;
$client->refreshToken($refreshToken);
$_SESSION['token'] = $client->getAccessToken();
}
if (isset($_SESSION['token'])) { // extract token from session and configure client
$token = $_SESSION['token'];
$client->setAccessToken($token);
}
if (!$client->getAccessToken()) { // auth call to google
$authUrl = $client->createAuthUrl();
header("Location: ".$authUrl);
die;
}

Related

How to make Exception much simpler CodeIgniter4

so, I have JWT authentication API using CI4, and one of the method is filtering which is telling if you has the token for JWT or not, if the user don't have the token it will return to an exception telling that JWT authentication is failed like this :
Postman :
Web :
is it possible to make it more simpler like this ?, just a response and without a trace exception like in the first image
Here is the
jwt_helper.php :
<?php
use App\Models\AuthModel;
use Firebase\JWT\JWT;
function getJWT($headerAuth)
{
if (is_null($headerAuth)) {
throw new Exception("JWT Authentication failed");
}
return explode(" ", $headerAuth)[1];
}
function validateJWT($encodedToken)
{
$key = getenv('JWT_SECRET_KEY');
$decodedToken = JWT::decode($encodedToken, $key, ['HS256']);
$authModel = new AuthModel();
// * xxxx.xxxx.xxxx
$authModel->getEmail($decodedToken->email);
}
function createJWT($email)
{
$requestTime = time();
$tokenTime = getenv('JWT_TOKEN_TIME');
$tokenExpireTime = $requestTime + $tokenTime;
$payload = [
'email' => $email,
'iat' => $requestTime,
'exp' => $tokenExpireTime
];
$jwt = JWT::encode($payload, getenv('JWT_SECRET_KEY'), 'HS256');
return $jwt;
}
This full stack is displayed only in DEVELOPMENT.
Edit your .ENV file accordingly.
CI_ENVIRONMENT = production
https://codeigniter.com/user_guide/general/environments.html#error-reporting

Error adding new event to google calender Google_Service_Exception (401) { "error": "unauthorized_client", "error_description": "Unauthorized" }

When i try to submit a new event to my google calender in a laravel project I always face this error:
Google_Service_Exception (401)
{ "error": "unauthorized_client", "error_description": "Unauthorized" }
I created new OAuth credentials for calendar api and added it to .env file as shown:
I am also using google+ api so that each user can access his calender to update his events via OAuth 2
I tried to add new OAuth credentials but the problem still persist I also tried to delegate domain-wide authority to my service account by adding client id added to the .env file & scope and authorize them but nothing changed I also waited for 24 hours waiting the changes to take place but also nothing changed
Here is the function am using to create new events:
public function doCreateEvent(Event $evt, Request $request)
{
$this->validate($request, [
'title' => 'required',
'calendar_id' => 'required',
'datetime_start' => 'required|date',
'datetime_end' => 'required|date'
]);
$title = $request->input('title');
$calendar_id = $request->input('calendar_id');
$start = $request->input('datetime_start');
$end = $request->input('datetime_end');
$start_datetime = Carbon::createFromFormat('Y/m/d H:i', $start);
$end_datetime = Carbon::createFromFormat('Y/m/d H:i', $end);
$cal = new \Google_Service_Calendar($this->client);
$event = new \Google_Service_Calendar_Event();
$event->setSummary($title);
$start = new \Google_Service_Calendar_EventDateTime();
$start->setDateTime($start_datetime->toAtomString());
$event->setStart($start);
$end = new \Google_Service_Calendar_EventDateTime();
$end->setDateTime($end_datetime->toAtomString());
$event->setEnd($end);
// Create new conference
$conference = new \Google_Service_Calendar_ConferenceData();
$entryPoint = new \Google_Service_Calendar_EntryPoint();
$entryPoint->setAccessCode('wx12z3s');
$entryPoint->setEntryPointType('video');
$entryPoint->setLabel('meet.google.com/wx12z3s');
$entryPoint->setMeetingCode('wx12z3s');
$entryPoint->setPasscode('wx12z3s');
$entryPoint->setPassword('wx12z3s');
$entryPoint->setPin('wx12z3s');
$entryPoint->setUri('https://meet.google.com/wx12z3s');
$conference->setEntryPoints($entryPoint);
$conferenceSolution = new \Google_Service_Calendar_ConferenceSolution();
$conferenceSolution->setIconUri(null);
$conferenceSolution->setKey(new \Google_Service_Calendar_ConferenceSolutionKey());
$conference->setConferenceSolution($conferenceSolution);
$conferenceRequest = new \Google_Service_Calendar_CreateConferenceRequest();
$conferenceRequest->setRequestId($request->_token);
$conferenceSolutionKey = new \Google_Service_Calendar_ConferenceSolutionKey();
$conferenceSolutionKey->setType("hangoutsMeet");
$conferenceRequest->setConferenceSolutionKey($conferenceSolutionKey);
$conferenceRequest->setStatus(new \Google_Service_Calendar_ConferenceRequestStatus());
$conference->setCreateRequest($conferenceRequest);
$event->setConferenceData($conference);
//attendee
if ($request->has('attendee_name')) {
$attendees = [];
$attendee_names = $request->input('attendee_name');
$attendee_emails = $request->input('attendee_email');
foreach ($attendee_names as $index => $attendee_name) {
$attendee_email = $attendee_emails[$index];
if (!empty($attendee_name) && !empty($attendee_email)) {
$attendee = new \Google_Service_Calendar_EventAttendee();
$attendee->setEmail($attendee_email);
$attendee->setDisplayName($attendee_name);
$attendees[] = $attendee;
}
}
$event->attendees = $attendees;
}
$created_event = $cal->events->insert($calendar_id, $event);
$evt->title = $title;
$evt->calendar_id = $calendar_id;
$evt->event_id = $created_event->id;
$evt->datetime_start = $start_datetime->toDateTimeString();
$evt->datetime_end = $end_datetime->toDateTimeString();
$evt->save();
return redirect('/event/create')
->with('message', [
'type' => 'success',
'text' => 'Event was created!'
]);
}
I am using a G suite account so that I can add events and assign hangout meet conferences to it but the problem keeps showing when i try to add newly created event to the user calender
The problem was the access token I am trying to use to access user calendar is wrong. When I deleted all user data and of course his access token saved to the database and tried to login again so that a new record created to the user with a new access token the problem solved and I can now access his calendar and create new events

laravel login with google account

I am making an application and I want users to login with their google account. I have user oauth-4-laravel and I have this:
UserController.php
// get data from input
$code = Input::get('code');
// get google service
$googleService = Artdarek\OAuth\Facade\OAuth::consumer("Google");
if (!empty($code)) {
// This was a callback request from google, get the token
$token = $googleService->requestAccessToken($code);
// Send a request with it
$result = json_decode($googleService->request('https://www.googleapis.com/oauth2/v1/userinfo'), true);
$user = DB::select('select id from users where email = ?', array($result['email']));
if (empty($user)) {
$data = new User;
$data->Username = $result['name'];
$data->email = $result['email'];
$data->first_name = $result['given_name'];
$data->last_name = $result['family_name'];
$data->save();
}
if (Auth::attempt(array('email' => $result['email']))) {
return Redirect::to('/');
} else {
echo 'error';
}
}
// if not ask for permission first
else {
// get googleService authorization
$url = $googleService->getAuthorizationUri();
// return to facebook login url
return Redirect::to((string) $url);
}
}
After this i get successfully user info and can save user name, in my database. The problem is that after this I want to redirect user to home page and can't do this because with normal login i chec authentication:
if (Auth::attempt(array('email' => Input::get('email'), 'password' => Input::get('password')))) {
return Response::json(["redirect_to" => "/"]);
and with google login i get onlu username , user id and email. How to login directly the user after google login?
If you need to log an existing user instance into your application, you may simply call the login method with the instance:
$user = User::find(1);
Auth::login($user);
This is equivalent to logging in a user via credentials using the attempt method.
For further info see: http://laravel.com/docs/security#manually

Magento User Sub-Accounts for Impersonation

I have a requirement for a Magento project that accounts are hierarchical. That is one account can "own" multiple other accounts. If one account owns another account it is allowed to impersonate that account: create orders, view account information, and view previous orders.
I'm not sure where to begin. If you have any thoughts, could you please point me in the right direction?
One solution would be to set up a Multiple Select attribute and populate it with the user ids of the users allowed to impersonate. You could then create either a separate php file that runs magento and logs in the user based on who they select, or integrate it into the cms.
Here is my custom 'login' code that lets my sso users from my Microsoft Database login to magento.
You call this function and pass it a 'user' you want to login as. Seems to work pretty well, however you will need to modify it to your needs. Don't expect it to work out of the box!
FYI: if you don't pass in all the junk that magento needs about the dispatchevents() then the user will not login properly. I had to reverse engineer this whole dern thing, so don't expect to see it anywhere else besides here and bits and pieces of magento core :)
$userId = 5;
$user = Mage::getModel('customer/customer')->load($userId)->setWebsiteId(1);
$this->LoginToMagento($user, null);
function LoginToMagento($user, $noAddress) {
// Must include this file in order to use the object
include ('/var/www/app/code/core/Mage/Customer/controllers/AccountController.php');
// Configure Magento to think its using the frontend
Mage::getSingleton("core/session", array("name" => "frontend"));
Mage::getConfig()->init();
Mage::getConfig()->loadEventObservers('frontend');
Mage::app()->addEventArea('frontend');
Mage::app()->loadArea(Mage_Core_Model_App_Area::AREA_FRONTEND);
// Grab the request and modify it with my emulated controller's information
$request = Mage::app()->getRequest();
$request->setRouteName('customer');
$request->setControllerModule('Mage_Customer');
$request->setRoutingInfo('');
$request->setModuleName('customer');
$request->setControllerName('account');
$request->setModuleKey('module');
$request->setControllerKey('account');
$request->setActionName('loginPost');
$request->setActionKey('action');
// Grab the response
$response = Mage::app()->getResponse();
// Feed the request and response into a new accountcontroller object
$accountControl = new Mage_Customer_AccountController($request, $response);
// Dispatch events related to the controller actions for predispatch
Mage::dispatchEvent('controller_action_predispatch', array('controller_action' => $accountControl));
Mage::dispatchEvent('controller_action_predispatch_customer', array('controller_action' => $accountControl));
Mage::dispatchEvent('controller_action_predispatch_customer_account_loginPost', array('controller_action' => $accountControl));
// Grab an instance of the customer session model
$session = Mage::getSingleton('customer/session');
try{
// Attempt to login the user
$session->setCustomerAsLoggedIn($user);
$session->renewSession();
} catch (Mage_Core_Exception $e) {
// Lets hope to never get here
$message = $e->getMessage();
error_log($message);
Mage::getSingleton('core/session')->addError($message);
}
// Perform the postdispatch events for 'after emulation of the controller'
Mage::dispatchEvent('controller_action_postdispatch_customer_account_loginPost', array('controller_action'=>$accountControl));
Mage::dispatchEvent('controller_action_postdispatch_customer', array('controller_action'=>$accountControl));
Mage::dispatchEvent('controller_action_postdispatch', array('controller_action'=>$accountControl));
$customer = Mage::getModel('customer/customer')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('entity_id', array('eq' => $user->getId()))
->getFirstItem();
try
{
// Prepare a collection of required values that the customer *should* have been set from netforum
$collection = Mage::getModel('eav/entity_attribute')->getCollection();
$collection->addFieldToFilter('entity_type_id', Mage::getModel('eav/entity')->setType('customer')->getTypeId());
// The var representing if validation has failed
$failedReq = false;
// Loop through each user defined required attribute and if we find one
// on the customer that is not set, forward the user to their account config page
foreach ($collection as $attribute)
{
if ($attribute['is_required'] && $attribute['is_user_defined'])
{
$attrCode = $attribute['attribute_code'];
if (!isset($customer[$attrCode]))
{
$failedReq = true;
}
}
}
// Try to determine where we logged in from (URL)
Mage::getSingleton("core/session", array("name" => "frontend"));
$session = Mage::getSingleton("customer/session");
$outputMessage = $session->getData('before_auth_url');
// Proceeed differently based on the existence of addresses
if ($noAddress == true)
{
if ($failedReq)
{
// Customer failed login. To be expected if they are signing in with SSO and never have before
$redirect_to = 'https://' . $_SERVER['HTTP_HOST'] . '/customer/account/edit/';
Mage::getSingleton('core/session')->addError('<b>Please fill in the required fields marked with * and click "Save"</b>');
header("Location: $redirect_to");
}
else
{
// Customer checks out ok, but has no addresses. Send them to the address setup screen
Mage::getSingleton('core/session')->addError('<b>Please fill in your address and phone number, then click "Save"</b>');
$redirect_to = 'https://' . $_SERVER['HTTP_HOST'] . '/customer/address/edit/';
header("Location: $redirect_to");
}
}
else
{
// Customer has addresses being passed from SSO
$defaultBillingId = $customer->getDefaultBillingAddress()->getId();
$hasPhoneNumber = false;
foreach ($customer->getAddresses() as $address)
{
$addrs = Mage::getModel('customer/address')->load($address->getId());
$magePhone = $addrs->getTelephone();
if ($magePhone)
{
$hasPhoneNumber = true;
}
}
if ($failedReq)
{
// Customer failed login. To be expected if they are signing in with SSO and never have before
$redirect_to = 'https://' . $_SERVER['HTTP_HOST'] . '/customer/account/edit/';
Mage::getSingleton('core/session')->addError('<b>Please fill in the required fields marked with * and click "Save"</b>');
header("Location: $redirect_to");
}
else
{
// Customer is has default values filled out
if (!$hasPhoneNumber)
{
// Phone number is missing for an address so redirect there and force em to enter it.
Mage::getSingleton('core/session')->addError('<b>Please fill in the required fields marked with * and click "Save Address"</b>');
$redirect_to = 'https://' . $_SERVER['HTTP_HOST'] . '/customer/address/edit/id/' . $defaultBillingId;
header("Location: $redirect_to");
}
else
{
// Everything is ok, so just try to send them back to where they came from, or the account screen
if ($outputMessage)
{
$redirect_to = $outputMessage;
}
else
{
$redirect_to = 'https://' . $_SERVER['HTTP_HOST'] . '/customer/account/';
}
header("Location: $redirect_to");
}
}
}
}
catch (Exception $e)
{
if ($outputMessage)
{
$redirect_to = $outputMessage;
}
else
{
$redirect_to = 'https://' . $_SERVER['HTTP_HOST'] . '/customer/account/';
}
header("Location: $redirect_to");
}
}
I know I am late but
http://amasty.com/sales-reps-and-dealers.html
This extension can be helpful to achieve what you are looking for. It will allow to create hierarchical accounts and assign the sales rep/sub admins to the orders and provide the access levels.
Hope this helps.

Basecamp API authentication with OAuth2: internal checksum failed error

I'm trying to write a CodeIgniter controller to handle OAuth2 authentication for the 37signals' Basecamp API.
The problem is I keep encountering the 'internal checksum failed' error, when trying to connect (via cURL) to https://launchpad.37signals.com/authorization.json, providing the Auth Token in a HTTP header.
Here's the index and _authcode functions from my controller class:
<?php
// constants:
// BC_REQUEST_URL = 'https://launchpad.37signals.com/authorization/new'
// BC_TOKEN_URL = 'https://launchpad.37signals.com/authorization/token'
// ...
public function index() {
// if get data is set.
if ($this->input->get()) {
// if auth code is provided via GET, switch to _authcode method.
if ( $code = $this->input->get('code') ) {
return $this->_authcode($code);
}
// On error, kill yourself.
if ( $error = $this->input->get('error') ) {
die($error);
}
}
// redirect to 37 signals to get an authcode
header("Location: ".BC_REQUEST_URL."?type=web_server&client_id=".BC_CLIENT_ID."&redirect_uri=".BC_REDIRECT_URL."");
}
// handles the Authentication code that is returned by 37 Signals.
private function _authcode($code) {
// set vars to POST
$vars = array(
'type' => 'web_server',
'client_id' => BC_CLIENT_ID,
'redirect_uri' => BC_REDIRECT_URL,
'client_secret' => BC_CLIENT_SECRET,
'code' => $code
);
// make a request for the access_token
$url = BC_TOKEN_URL;
$c = curl_init($url);
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, http_build_query($vars));
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
$response = json_decode(curl_exec($c));
curl_close($c);
unset($c,$url);
// get the access vars from this request
$expiry_seconds = $response->expires_in; // default: 1209600 (14 days)
$refresh_token = $response->refresh_token;
$access_token = $response->access_token;
unset($response);
// make a separate request to get user info for current user.
$url = "https://launchpad.37signals.com/authorization.json";
$c = curl_init($url);
curl_setopt($c, CURLOPT_HTTPHEADER, array(
"Authorization: Bearer <$access_token>",
"Content-Type: application/json; charset=utf-8",
"User-Agent: MyApp (http://myapp.example.com)"
));
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
$response = json_decode(curl_exec($c)); // reply from 37 signal auth
curl_close($c);
unset($c,$url);
echo "response obj = " . print_r($response,1);
/* prints: response obj = stdClass Object ( [error] => OAuth token could not be verified. The internal checksum failed, so the token data was somehow mangled or tampered with. ) */
// get the user data from this request
// $expires_at = $response->expires_at; // the timestamp for when this request expires
// $identity = $response->identity; // the current user
// $accounts = $response->accounts; // list of accounts we can access
// unset($response);
// store the response data to the database for easy recall.
// $this->db->query("REPLACE INTO `sometable` SET `key1`='value', `key2`='value');
}
// ...
?>
I ran into this error when saving the auth token in the database with varchar(255). Basecamp's auth token has some checksum data which brings the token over 255 characters.
You don't appear to be pulling it from a database in your example, so this might not affect you, however checking for Basecamp's token being cut off is what I would first look at.
Optionally, remove the <> characters around your $access_token when setting the Bearer header.

Resources