google-api-php-client apiAuthException refreshing OAuth2 - php

I'm developing a PHP script to display a page with some charts and stats about my Analytics profile. I'm sending 3 or 4 ajax asynchronous calls to the server; the server then fetch data using google-api-php-client . There are about 10 requests to Google for each page refresh.
The problem is i'm getting totally random exceptions from the api:
Fatal error: Uncaught exception 'apiAuthException' with message 'Error
refreshing the OAuth2 token, message: ' in apiOAuth2.php on line 242
One time i successfully get the data, one time not. This is very annoying because i cannot find why. What could be? The asynch calls maybe?
User quota is set to 1000 requests/second just to be sure. Eventually i get 'User quota exceded' from the api, something quite impossible.

I answer to myself because i found the problem (was my bad):
I store the access token into an encrypted file
Even if the access token should be offline it's not really offline: it expires but contains a refresh token that APIs will send to get a new fresh access token
The new access token should be saved again into the file (i save it into __destruct() method of my wrapper class) and used for the next request (i was not saving it!)

Related

Google Calendar API- API key is no longer valid without reason (400)

I have an application (php) that handles the API for years.
However a few weeks ago it started giving the error sporadically:
(400) API key not valid. Please pass a valid API key.
In the end the error was continuous and stopped responding.
I related it to Google Calendar API - no longer authorized for reads?
After several days of inactivity, the application worked again for a few days, but the pattern has been repeated again:
It has been giving the error more and more frequently until it has stopped working.
Edited:
The application can be viewed at:
http://intraneteina.unizar.es/intraneteina/index.php?r=calendarioGoo/index
When selecting any option from the dropdown, the application reads information from a google calendar and displays it in html.
It has been working for years, and without touching the code, now it gives the described API error key not valid.
I had been experiencing a similar issue. I found some documentation, eventually, that said there was a limit of 50 (or possibly 100) "active" authorisation tokens allowed for an app unless you are running it via a service account. Typically once the limit is reached Google just drops authorisations off the end of the "active list" it maintains and you are unaware of this happening. This is a problem if you are relying on the expiry of your access token and don't generate a refresh token until you think you need to - i.e. When you generate/refresh your access token you record the expiry date and only refresh it when you system tells you that your token has expired.
Because Google may have disabled that token (dropped it off the active list) in the background and you are unaware of this you try to use that token which hasn't expired - the result is a typically uninformative error message with no indication of what has happened. In our case in the short term setting up a service account was too difficult (implementing RSA256 for the authorisation process) so we got around this by just ignoring the recorded expiry timestamp and requesting a refresh token every time we made a call to the API. I'm sorry I can't link you to the documentation, but I believe I found it via an answer in another S.O. post.

Podio authentication fails after multiple calls

I've been working with the Podio API for nearly a year now and have rarely had issues, but I recently ran into one that I can't figure out. I'm not even sure how to test what is going on. When an item is created, I have a webhook to a script which will send an http GET request to an item and perform a number of functions. The script runs just fine and has never run into any issues, unless I create a number of items in quick succession (15 or so). If I do this, a certain number will finish successfully, and then I will suddenly get the following error with each new call:
2016-11-14 16:41:14 401 GET /item/514610204
2016-11-14 16:41:14 Reponse: {"error_parameters":{},"error_detail":null,"error_propagate":false,"request":{"url":"http:\/\/api.podio.com\/item\/514610204","query_string":"","method":"GET "},"error_description":"invalid_request","error":"unauthorized"}
If I wait a minute or so, it begins working again.
I'm authenticating with username and password. Does anyone know what is going on or how to check what is going on? The podio.log has not been helpful in this situation.
(UPDATE)
The issue is that I am hitting an authentication request rate limit because I haven't been using a session manager. I'm now attempting to do this, but having issues. My code using the Redis setup is as follows:
require_once 'models/PodioRedisSession.php';
Podio::set_debug(true, 'file');
Podio::setup($client_id, $client_secret,array(
"session_manager" => "PodioRedisSession"
));
Podio::$auth_type = array(
"type" => "password",
"identifier" => "MY_EMAIL"
);
Podio::$oauth = self::$session_manager->get(Podio::$auth_type); //ERROR IS ON THIS LINE
if (!Podio::is_authenticated()) {
Podio::authenticate_with_password('MY_EMAIL', 'MY_PASSWORD');
}
which gives me the following error: Cannot access self:: when no class scope is active.
Am I on the right track to get the session manager working with password authentication? The error occurs on the commented line.
(UPDATE)
I was finally able to get the system running properly. The code above is correct. The server just needed to be reset (Windows server) for Redis to take affect.
If your script is authenticating to Podio each time an item is created, it is possible that you are running into the Podio API’s rate limit on authentication requests.
Are you generating a new auth token each time the script is triggered by your webhook? The Podio client libraries include some general documentation on session management that may be useful!
To avoid hitting this limit you should authenticate with the API once and then store the oauth and refresh tokens that at are returned from the API. Redis will do the trick. Then you use the oauth token for all subsequent requests.
An example can be found here [1]
Your oauth token will be valid for at most 28 days. When it's no longer valid you will need to obtain a new oauth token using the refresh token you received when you first authenticated. [2]
[1] https://developers.podio.com/authentication/username_password
[2] https://developers.podio.com/authentication

FacebookAuthorizationException' with message Unsupported get request - Facebook PHP API

This url:
https://graph.facebook.com/v2.2/389546181070007/feed?access_token={token}&appsecret_proof={secret}&fields=name
And:
https://graph.facebook.com/v2.2/186726524843032/feed?access_token={token}&appsecret_proof={secret}&fields=name
have both valid pageID's and the same access_token and appsecret. The token I'm getting with:
https://graph.facebook.com/oauth/access_token?client_id={app_id}&client_secret={app_secret}&grant_type=client_credentials'
Yet the first one throws an exception and the second one works fine and returns the feed list.
The exception thrown is: FacebookAuthorizationException' with message 'Unsupported get request.
Have tried to use a different app and more things... been struggling with this issue for 6 hours now.
Has anyone any idea how it is possible that one page works and the other doesn't?
When I try to visit the first page on Facebook, I only get “Sorry, this page isn't available”.
That means that either that page is not published yet, or has some access restrictions in place. In both cases, you can not “see” it via API, when you use an app access token.
You will need to use either a page access token for that page, or a user access token for a user that has the right to see the page.

QuickBooks IPP v3 getting error 3200 when adding Invoice

I've been using the QuickBooks PHP DevKit for some time now in production with no issues.
However, recently I've been getting the following error from my create_invoice ajax script when I spit out:
if($resp = $InvoiceService->add($Context, $realm, $Invoice)){
//create invoice code here
} else
{
echo $InvoiceService->lastError();
}
I get the following error:
3200: [message=ApplicationAuthenticationFailed; errorCode=003200; statusCode=401, ]
The only documentation I can find about this error is that error code 3200 means "Outdated edit sequence" but I am creating an Invoice.
So why is this error all of a sudden appearing?
It seems to have started giving me this error since July 11th. Before then everything was working fine.
Do I need to refresh my access tokens, and how would I go about doing that?
The only documentation I can find about this error is that error code 3200 means "Outdated edit sequence" but I am creating an Invoice.
Error code 3200 (for qbXML) is entirely separate from error code 003200 (for IPP/v3), so "outdated edit sequence" isn't really relevant here at all.
So why is this error all of a sudden appearing?
Generally a "401 Authentication Failed" error pretty much means exactly what it says - authentication against the app failed.
This could be because of several reasons:
Your OAuth tokens expired (they expire after 6 months if you don't renew them)
Your OAuth tokens/connection were disconnected (this can be done from within the UI Intuit provides)
You're not sending the correct OAuth tokens anymore
Did you check your OAuth tokens? Are they still valid? Are you sending the correct ones?
Do I need to refresh my access tokens, and how would I go about doing that?
Maybe.
You can go back through the connection process (e.g. click the "Connect to QuickBooks" button again) to renew them.
If they have expired (e.g. you didn't renew within 6 months) you should implement the reconnect call to avoid this in the future:
https://github.com/consolibyte/quickbooks-php/blob/master/docs/partner_platform/example_app_ipp_v3/reconnect.php
https://github.com/consolibyte/quickbooks-php/blob/master/docs/partner_platform/example_app_ipp_v3/
https://github.com/consolibyte/quickbooks-php

Facebook session problem

I am trying to connect to a facebook API, but found some issues.
The user has to log in by the javascript FB.login function. Then a cookie is stored and the PHP class will try to connect with this cookie.
When I test on my local machine, everything works fine. When I upload my code to the webserver and try out my code, the connection is done with javascript. It places the cookie with the right data on my computer. When I parse the cookie, the access_token is set, but there is no way of connecting to facebook. I receive the error:
Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user
I tried to delete my cookies, so a new session is created, but I still get the same error. THe api key and secret are correct, so that cannot be the problem. I can post the code here, but it is the basic API code.
Does somebody know how I can solve this problem?
Thanks in advance!
Wow... The javascript attaches some / and " chars at the beginning and the ending of the cookie. Looked over it. Was looking on this issue for about 3 hours :D
trim($cookie, '/"');
resolved my problem.

Categories