Google PHP API Offline Access Issue - google-api

I am trying to use Google PHP API to sream contents from Google plus. I was able to get the refresh token. But when I tried to get access token using that refresh token, its returning the following error.
Fatal error: Uncaught exception 'apiAuthException' with message 'Error refreshing the OAuth2 token, message: '{ "error" : "invalid_grant" }'' in /var/www/social-media-test/google-api-php-client/src/auth/apiOAuth2.php:242 Stack trace: #0 /var/www/social-media-test/google-api-php-client/src/apiClient.php(281): apiOAuth2->refreshToken('MY-REFRESH-TOKEN-HERE') #1 /var/www/social-media-test/google-api-php-client/examples/plus/p.php(19): apiClient->refreshToken('MY-REFRESH-TOKEN-HERE') #2 {main} thrown in /var/www/social-media-test/google-api-php-client/src/auth/apiOAuth2.php on line 242
MY code is as shown below.
$client = new apiClient();
$client->setApplicationName('Google+ PHP Starter Application');
$client->setClientId('client-id');
$client->setClientSecret('secret-key');
$client->setRedirectUri('http://localhost/index.php/main');
$client->setDeveloperKey('dev-key');
$plus = new apiPlusService($client);
$client->refreshToken('MY-REFRESH-TOKEN');
Any Ideas?

you forgot this:
$client->setScopes(array(
'https://www.googleapis.com/auth/blogger'
));
ref: https://developers.google.com/gdata/docs/auth/oauth#Scope

Related

`406 Not Acceptable` response: Error 406 - Not Acceptable

I uploaded laravel5.6 project on server and got this error. Running well on local machine.
when we request api using guzzle with laravel throwing error. Below is my code requesting api.
try{
$response = $this->client->request('GET',$this->url.'index');
$response = $response->getBody()->getContents();
}catch (\GuzzleHttp\Exception\ClientException $e) {
echo 'Caught response: '.$e;
}
returns
Caught response: GuzzleHttp\Exception\ClientException: Client error: GET http://example.com/api/index resulted in a 406 Not Acceptable response:Error 406 - Not Acceptable
How to get solve this error

Laravel socialite 400 Bad Request response

i have created a laravel socialte setup .and it was working before perfectly now its showing error(below).
1)i have changed client_secret
2)created a new oauth credentials
still not working
public function redirectToGoogle()
{
return Socialite::driver('google')->redirect();
}
public function handleGoogleCallback()
{
$user = Socialite::driver('google')->stateless()->user();
$user->getId(); // 1472352
$user->getNickname(); // "overtrue"
$name= $user->getName(); // "安正超"
$emailid= $user->getEmail();
$pic= $user->getAvatar(); // "anzhengchao#gmail.com"
return->redirect('welcome');
}
i have created env file with client_secret and client id
"""
Client error: `POST https://accounts.google.com/o/oauth2/token` resulted in a `400 Bad Request` response:\n
{\n
"error" : "invalid_grant",\n
"error_description" : "Code was already redeemed."\n
}\n
"""
When Google return the Authentication Code code to your Socialite, it can only be used to exchange to Access Token once. Doing more than once will result in the error Code was already redeemed.
The flow should be:
User click the login button on your website
You redirect user to Google and Google is asking user to login/grant you access
If successful, Google redirects back to you with a one-time-use Authentication Code?code=.....
Socialite use the ?code and exchange it with Google to get user's Access Token. This can only be done once per flow.
You can now request user details using the access token requested in step 4.
Read similar answer: https://stackoverflow.com/a/32710034/534862

Google places API returning failed to open stream: HTTP request failed error

I am hoping for some direction on this error. I have a loop that connects with the google places API. Every few times I test it I get an HTTP request failed error. But when I take the API call from the error it works in a browser or in postman. Any suggestions for what might be causing this? Thanks for all of your help. Here is my API call and the error.
$googleApiPlaceUri = 'https://maps.googleapis.com/maps/api/place/textsearch/json';
foreach ($attractions as $attraction) {
$api_call = "{$googleApiPlaceUri}?query={$attraction}+in+{$city}&key={$api_key}";
$api_return_array[] = json_decode(file_get_contents($api_call));
foreach ($api_return_array as $result) {
$results = $result->results;
array_push($results_array, $results);
}
}
Here is the error I get...
Warning: file_get_contents(https://maps.googleapis.com/maps/api/place/textsearch/json?query=hiking +in+Denver&key=API_KEY): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request

How to get data from API google?

I have access_token and now to try get fata from Google API using this sample:
https://developers.google.com/android-publisher/api-ref/edits/details/get
I try to get data executing query to this URL:
https://www.googleapis.com/androidpublisher/v2/applications/packageName/edits/editId
$curl = new Curl();
$curl->get('https://www.googleapis.com/androidpublisher/v1/applications/com.shazam.android&access_token='.$_SESSION['access_token']["access_token"]);
Where $_SESSION['access_token']["access_token"] is access_token
As a result I get message: Error: 404: HTTP/1.1 404 Not Found
How to get information from API?
This is full URL request, it does not work:
https://www.googleapis.com/androidpublisher/v2/applications/com.shazam.android/edits/1?access_token=ya29.Ci-5Azg5JoBiESM5cEzM3TQrP3SXHAFirFg0f-Fj83PxtrtINWQDe2L-3f5wP7oAtQ
{
"error": {
"errors": [
{
"domain": "global",
"reason": "insufficientPermissions",
"message": "Insufficient Permission"
}
],
"code": 403,
"message": "Insufficient Permission"
}
}
Full code is:
// Create the client object and set the authorization configuration
// from the client_secretes.json you downloaded from the developer console.
$client = new Google_Client();
$client->setAuthConfig(__DIR__ . '/client_secrets.json');
$client->addScope(Google_Service_Analytics::ANALYTICS_READONLY);
// If the user has already authorized this app then get an access token
// else redirect to ask the user to authorize access to Google Analytics.
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
// Set the access token on the client.
$client->setAccessToken($_SESSION['access_token']);
$curl = new Curl();
$curl->get('https://www.googleapis.com/androidpublisher/v1/applications/com.shazam.android/edits/1/&access_token='.$_SESSION['access_token']["access_token"]);
if ($curl->error) {
echo 'Error: ' . $curl->errorCode . ': ' . $curl->errorMessage . "\n";
} else {
echo 'Response:' . "\n";
var_dump($curl->response);
}
// Create an authorized analytics service object.
//$analytics = new Google_Service_Analytics($client);
// Get the first view (profile) id for the authorized user.
//$profile = getFirstProfileId($analytics);
// Get the results from the Core Reporting API and print the results.
//$results = getResults($analytics, $profile);
//printResults($results);
} else {
$redirect_uri = 'http://localhost/play/oauth2callback.php';
header('Location: ' . $redirect_uri);
}
After getting access_token I try to make Curl request:
$curl = new Curl();
$curl->get('https://www.googleapis.com/androidpublisher/v1/applications/com.shazam.android/edits/1/&access_token='.$_SESSION['access_token']["access_token"]);
Well, another possible caused of error 403 or Insufficient Permission is that the scope that you are using is incorrect or you have not requested the scopes you need when you retrieved your access token. So try to double check the scope that you are using and enable all the API in the Dev Console that you are using.
You can check which scopes you have requested by passing your access_token to this endpoint:
https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=ACCESS_TOKEN
For more information, check the solution in these related SO question if it can help you.
Permission issue Google Play
Google API 'Insufficient Permission'?

Socialite in Laravel 5.1

I'm trying Socialite with laravel 5.1 and I always got this error during call back route
InvalidStateException in AbstractProvider.php line 200:
and this is my code for callback route
public function handleProviderCallback($provider) {
$user = Socialite::driver($provider)->user();
}
additional information about issue
I saw this error happened when I authenticated from facebook only , when I disable this code in AbstractProvider.php line 200
throw new InvalidStateException;
Show new error
cURL error 7: Failed to connect to graph.facebook.com port 443

Resources