I am working with google api(Rest Api) in php, I just want whenever
any user send "access_token/token" then i want to validate and get userinfo,How can i do this ?
I tried with following code but not working for me,showing me always url in my postman,Where i am wrong ?
<?php
$google_client->authenticate($_GET['code']);
$client = new Google_Client();
$google_client = new Google_Client();
$google_client->setClientId('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com'); //Define your ClientID
$google_client->setClientSecret('xxxxxxxxxxxxxxxxxxxxx'); //Define your Client Secret Key
$google_client->setRedirectUri('xxxxxxxxxxxx'); //Define your Redirect Uri
$google_client->addScope('email');
$google_client->addScope('profile');
$objOAuthService = new Google_Service_Oauth2($google_client);
$getAccesToken = $google_client->getAccessToken();
$getRefreshToken = $google_client->getRefreshToken();
$google_client->setAccessToken($token['access_token']);
$objOAuthService = new Google_Service_Oauth2($google_client);
if($google_client->getAccessToken()) {
$userData = $objOAuthService->userinfo->get();
if(!empty($userData))
{
$outputjson['user_data'] = $userData;
$outputjson['access_token'] = $google_client->getAccessToken();
}else{
$outputjson['login_url'] = $google_client->createAuthUrl();
}
}
I've setup stripe multiple times and never had this issue before. I'm processing the "customer created" web hook and using the payload to attach stripe id to my user and setup a new subscription.
I'm getting the following error:
Stripe\Exception\UnexpectedValueException: Could not determine which URL to request: Stripe\Subscription instance has invalid ID: in file /Users/mick/repos/askit/vendor/stripe/stripe-php/lib/ApiResource.php on line 107
This only happens when I try to create the new subscription. Here is my full controller:
public function handleCustomerCreated(Request $request)
{
$payload = json_decode($request->getContent(), true);
$user_id = $payload->data->object->subscriptions->data[0]->metadata->user_id;
$subscription_id = $payload->data->object->subscriptions->data[0]->id;
$stripe_customer_id = $payload->data->object->id;
$current_period_start = $payload->data->object->subscriptions->data[0]->current_period_start;
$current_period_end = $payload->data->object->subscriptions->data[0]->current_period_end;
$stripe_plan = $payload->data->object->subscriptions->data[0]->items->data[0]->plan->id;
$name = $payload->data->object->subscriptions->data[0]->items->data[0]->plan->nickname;
$stripe_status = $payload->data->object->subscriptions->data[0]->status;
$interval = $payload->data->object->subscriptions->data[0]->items->data[0]->plan->interval_count;
$user = User::query()->find($user_id);
$user->stripe_id = $stripe_customer_id;
$user->save();
$subscription = new Subscription();
$subscription->user_id = $user_id;
$subscription->name = $name;
$subscription->stripe_id = $stripe_customer_id;
$subscription->stripe_status = $stripe_status;
$subscription->stripe_plan = $stripe_plan;
$subscription->interval = $interval;
$subscription->current_period_start = $current_period_start;
$subscription->current_period_end = $current_period_end;
$subscription->subscription_id = $subscription_id;
$subscription->save();
}
If I remove the new Subscription stuff it works fine, the stripe is getting attached to my user model just fine. The error only happens on this part:
$subscription = new Subscription();
$subscription->user_id = $user_id;
$subscription->name = $name;
$subscription->stripe_id = $stripe_customer_id;
$subscription->stripe_status = $stripe_status;
$subscription->stripe_plan = $stripe_plan;
$subscription->interval = $interval;
$subscription->current_period_start = $current_period_start;
$subscription->current_period_end = $current_period_end;
$subscription->subscription_id = $subscription_id;
$subscription->save();
Any ideas? The table was created automagically by Laravel Cashier.
Try using \Stripe\Subscription::create() as shown in the Stripe API Docs, instead of manually creating and modifying a new Subscription(). You're current code is trying to update a Subscription that doesn't yet exist in the API, so it can't find its id.
With the following code using Google's PHP API Client I am receiving this response.
Google_Service_Exception with message 'Error calling POST https://www.googleapis.com/dns/v1/projects/PROJECT-NAME/managedZones/DNSZONE/changes: (400) The 'entity.change' parameter is required but was missing.
Where PROJECT-NAME and DNSZONE are my project and zone.
$client_email = MYCLIENT;
$private_key = file_get_contents('config/credentials/KEY.p12');
$scopes = array('https://www.googleapis.com/auth/ndev.clouddns.readwrite');
$project = "PROJECT-NAME";
$managedZone = "DNSZONE";
$creds = new Google_Auth_AssertionCredentials($client_email,$scopes,$private_key);
$client = new Google_Client();
$client->setAssertionCredentials($creds);
$resource = new Google_Service_Dns_ResourceRecordSet();
$resource->kind = "dns#resourceRecordSet";
$resource->name = "testing.DNSZONE.net.";
$resource->rrdatas[] = "testing.otherhost.com.";
$resource->ttl = 800;
$resource->type = "CNAME";
$dns = new Google_Service_Dns($client);
$change = new Google_Service_Dns_Change();
$change->kind = "dns#change";
$change->setAdditions($resource);
$dns->changes->create($project,$managedZone,$change);
I am a bit confused as to how to set this parameter. Or where I am even am to define it.
Just for clarify what the answer is, setAdditions expects an array.
$change->setAdditions([ $resource ]);
I am trying to set some basic example of using Google Analytics with this library: https://github.com/google/google-api-php-client
For starter I have:
<?php
require_once 'Google/Client.php';
require_once 'Google/Service/Analytics.php';
$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
$client->setDeveloperKey("MY_SECRET_API"); //security measures
$service = new Google_Service_Analytics($client);
$results = $service->data_ga;
echo '<pre>';
print_r($results);
echo '</pre>';
Q: How to get data from Google Analytics from this query ?
/*
https://www.googleapis.com/analytics/v3/data/
ga?ids=ga%123456
&dimensions=ga%3Acampaign
&metrics=ga%3Atransactions
&start-date=2013-12-25
&end-date=2014-01-08
&max-results=50
*/
$client->setDeveloperKey("MY_SECRET_API");
First of all, for as far as I experienced this won't work for authentication, you'll need to use a OAuth2 authentication. There are two options to do this, using client ID for web application or using a service account. Authorization api
After you have this, you can make a call like this.
(I use a service account here)
First authenticate:
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
$service_account_name,
array('https://www.googleapis.com/auth/analytics.readonly'),
$key
);
$client->setAssertionCredentials($cred);
Make a call:
$ids = 'ga:123456'; //your id
$startDate = '2013-12-25';
$endDate = '2014-01-08';
$metrics = 'ga:transactions';
$optParams = array(
'dimensions' => 'ga:campaign',
'max-results' => '50'
);
$results = $service->data_ga->get($ids, $startDate, $endDate, $metrics, $optParams);
//Dump results
echo "<h3>Results Of Call:</h3>";
echo "dump of results";
var_dump($results);
echo "results['totalsForAllResults']";
var_dump($results['totalsForAllResults']);
echo "results['rows']";
foreach ($results['rows'] as $item) {
var_dump($item);
}
You will need to do a http get to get the information from the url.
http://www.php.net/manual/en/function.http-get.php
Remember you will still need to add the Oauth2 auth code to the string before you can send that request. This link might help if you dont have auth code already.
https://developers.google.com/analytics/solutions/articles/hello-analytics-api#authorize_access
what you could do is create a new function...
function ga_campaign_transactions($gaEmail, $gaPass, $gProfile, $limit)
{
require_once('classes/google-analytics/gapi.class.php');
$gDimensions = array('campaign');
$gMetrics = array('transactions');
$gSortMetric = NULL;
$gFilter = '';
$gSegment = '';
$gStartDate = '2013-12-25';
$gEndDate = '2014-01-08';
$gStartIndex = 1;
$gMaxResults = $limit;
$ga = new gapi($gaEmail, $gaPass);
$ga->requestReportData($gProfile, $gDimensions, $gMetrics, $gSortMetric, $gFilter, $gSegment, $gStartDate, $gEndDate, $gStartIndex, $gMaxResults);
$gAnalytics_results = $ga->getResults();
//RETURN RESULTS
return $gAnalytics_results;
}
$gProfile = '123456'; // The Profile ID for the account, NOT GA:
$gaEmail = 'YOUR GOOGLE EMAIL'; // Google Email address.
$gaPass = 'YOUR GOOGLE PASSWORD'; // Google Password.
// NOTE: if 2 step login is turned on, create an application password.
$limit = 50;
$ga_campaign_transactions = ga_campaign_transactions($gaEmail, $gaPass, $gProfile, $limit)
//OUTPUT
if(!empty($ga_campaign_transactions))
{
$counter=0;
$gaCampResults= array(); // CREATE ARRAY TO STORE ALL RESULTS
foreach($ga_campaign_transactions as $row)
{
$dim_list = $row->getDimesions();
$met_list = $row->getMetrics();
$gaCampResults[$counter]['campaign'] = $dim_list['campaign'];
$gaCampResults[$counter]['transactions'] = $met_list['transactions'];
$counter++;
}
}
if(!empty($gaCampResults))
{
$totalCampTransactions = count($gaCampResults);
?>
<h2>We Found ( <?php echo number_format($totalCampTransactions,0);?> ) Results</h2>
<ul>
<?php
foreach($gaCampResults as $gaRow){
echo "<li>Campaign:".$gaRow['campaign']." | Transactions: ".$gaRow['transactions']."</li>";
}
?>
</ul>
<?php
}
find Analytics Profile ID
Create Google Application password
Hopefully that puts you on the right track :)
untested this, but similar to what I've been using...
Marty
I've got my session with the valid token that i set up this way :
$session_token = Zend_Gdata_AuthSub::getAuthSubSessionToken($_GET['token']);
// Store the session token in our session.
$_SESSION['cal_token'] = $session_token;
Then i want to be able to do this:
$service = Zend_Gdata_Docs::AUTH_SERVICE_NAME;
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
$docs = new Zend_Gdata_Docs($client);
$feed = $docs->getDocumentListFeed();
But using the token. Instead of the authentication with user/pass/service
I already looked at some example of this but i didn't find any way to make it work.
Thank you everyone!
// Retrieve user's list of Google Docs
$client = Zend_Gdata_AuthSub::getHttpClient($_SESSION['cal_token']);
$docs = new Zend_Gdata_Docs($client);
$feed = $docs->getDocumentListFeed();
foreach ($feed->entries as $entry) {
echo "$entry->title\n";
}