Problem in Google Translater API - php

I am doing Language translation using Google Translater API.
Sometimes I am getting following error.
Fatal error: Uncaught GTranslateException: [0]: Unable to perform Translation:Suspected Terms of Service Abuse. Please see http://code.google.com/apis/errors thrown in C:\xampp\htdocs\MyProjectName\public\function\GTranslate.php on line 263
Can anybody help me to fix this problem.
Thanks in advance.
Kanji.

Sign up for google API Key at https://code.google.com/apis/console/ as mentioned earlier.
require_once("GTranslate.php");
try{
$gt = new Gtranslate();
$gt->setApiKey('YOUR_GOOGLE_API_KEY');
$gt->setUserIp($_SERVER['REMOTE_ADDR']);
echo $gt->english_to_german("hello world");
} catch (GTranslateException $ge)
{
echo $ge->getMessage();
}
Also consider to provide user IP address. This also can increase your daily limit.

Sign up for a Google API key at https://code.google.com/apis/console/ and activate the Google Translate API. Use the provided key in your project.
From the Google API Console page, follow on the traffic controls link on the left, then click the Configure Traffic Controls button. Increase the characters/second/user for Google Translate to something much larger than the default 100. Keep in mind you have a limit of 100,000 characters total per day.
Edit: as mentioned in my comment, this is how you would set the API key.
$apikey = 'xyz123';
$gt = new Gtranslate();
$gt->setApiKey($apikey);

Related

Callback error on YouTube socialite provider for Laravel

I am trying to set up Oauth with the YouTube Data API. I had a Laravel app which has Socialite set up. Out of the box YouTube isn't set up with this but I saw that there is a provider for YouTube here:
https://socialiteproviders.netlify.app/providers/you-tube.html
I have done all of the steps outlined on the page along with all routes that I need. I have also done the Oauth set up on Google Developer Console and got the client ID/secret key and set the callback.
When I use the login URL it works where I'm redirected for login with Google. The problem comes when the callback URL is reached. I get the error:
ErrorException
Undefined index: items
This occurs on the provider callback function which has the code:
$user = Socialite::driver('youtube')->user();
I have tried using stateless:
$user = Socialite::driver('youtube')->stateless()->user();
But get the same error. All caches have been cleared. I am pretty sure that the setup was done correctly as I'm also using the Twitch provider from https://socialiteproviders.netlify.app/providers/twitch.html which the setup was similar and it works correctly.
Please can anyone advise? Thanks.
Try selecting the fields you want to access first:
$user = Socialite::driver('youtube')->fields([
'items'
])->user();
I'm facing the same issue. Is it possible that the API has changed? If I take a look at the raw response there
I also stumbled onto this issue:
When I tested it, I did not got the error, but my colleague did so I figured it had something to do with the account that tried to connect.
I changed my approach from:
$user = Socialite::driver('youtube')->stateless()->user();
And just received tokens by doing this:
$socialite = Socialite::driver('youtube');
$code = $request->input('code');
$response = $socialite->getAccessTokenResponse($code);
$response will contain an array of tokens. I used these tokens to connect it to an existing user in my database.
I don't know if this is the solution for your workflow, but it is a way to get around the mysterious error.
The issue is due to YouTube no longer automatically creating a channel for your google/gmail account like it did in the past. This results in responses completely missing an items array.
if you dd($response->getBody()->getContents()) the response for an account that throws an error you'll see this.
I've made a pull request for this here. https://github.com/SocialiteProviders/YouTube/pull/8

PHP OpenWeather

Alright so I am having some troubles understanding how to use the open weather API.
I have tried using it as
<?php
$request = file_get_contents('http://api.openweathermap.org/data/2.5/forecast/city?id=myidblablabla'); //example ID
$jsonPHP = json_decode($request);
echo $jsonPHP->city;
?>
But I get an error saying
Catchable fatal error: Object of class stdClass could not be converted
to string in
Now there is 1 more question that I have to ask, how do I get certains City Temperature, humidity etc? From the code I received I get only Moscow
To simplify it, you can also transform the json into array instead.
$jsonPHP = json_decode($request,true);
Now let's discuss it briefly. According to the documentation (http://openweathermap.org/current),
Note also that I've never used this API before. I'm just trying to help here.
If you hit api.openweathermap.org/data/2.5/weather?lat=35&lon=139
It responds as
{"coord":{"lon":139,"lat":35},
"sys":{"country":"JP","sunrise":1369769524,"sunset":1369821049},
"weather":[{"id":804,"main":"clouds","description":"overcast clouds","icon":"04n"}],
"main":{"temp":289.5,"humidity":89,"pressure":1013,"temp_min":287.04,"temp_max":292.04},
"wind":{"speed":7.31,"deg":187.002},
"rain":{"3h":0},
"clouds":{"all":92},
"dt":1369824698,
"id":1851632,
"name":"Shuzenji",
"cod":200}
Now assuming you want to take the weather and humidity , it just :
weather :
echo $jsonPHP["weather"][0]["id"];
humidity:
echo $jsonPHP["main"]["humidity"];
Note also that, If you hit http://api.openweathermap.org/data/2.5/weather?lat=35&lon=139 and get the responds as
{"cod":401, "message": "Invalid API key. Please see http://openweathermap.org/faq#error401 for more info."}
For the case, they've explained here :
http://openweathermap.org/faq#error401 which is :
Q: API calls return an error 401
A: Starting from 9 October 2015 our
API requires a valid APPID for access. Note that this does not mean
that our API is subscription-only now - please take a minute to
register a free account to receive a key.
We are sorry for inconvenience but this is a necessary measure that
will help us deliver our services to you faster and more reliably.
For FOSS developers: we welcome free and open source software and are
willing to help you. If you want to use OWM data in your free software
application please register an API key and file a ticket describing
your application and API key registered. OWM will review your request
lift access limits for your key if used in open source application.

Google Translate API Access Error: (403) Access Not Configured

This is my first attempt to use the Google API, so I'm probably missing something ridiculously simple.
My API access has been turned on, I've enabled all the web based settings I could find for this feature, setup the Simple API Access and Client ID for web applications, etc.
I'm using the PHP library Google provide here: http://code.google.com/p/google-api-php-client/
As my code wasn't working, I defaulted to their sample code as getting that working usually solves my problems. I've copied their code below:
require_once '../../src/Google_Client.php';
require_once '../../src/contrib/Google_TranslateService.php';
$client = new Google_Client();
$client->setApplicationName('Translation tool');
// Visit https://code.google.com/apis/console?api=translate to generate your
// client id, client secret, and to register your redirect uri.
$client->setDeveloperKey('XXXXXXXXXXXXXXXX');
$service = new Google_TranslateService($client);
$langs = $service->languages->listLanguages();
print "<h1>Languages</h1><pre>" . print_r($langs, true) . "</pre>";
$translations = $service->translations->listTranslations('Hello', 'hi');
print "<h1>Translations</h1><pre>" . print_r($translations, true) . "</pre>";
I have:
checked the developer key, which is the API key
checked the allowed referers
Set the referers to include .DOMAIN.com/
Set the referers to include the IP address
Tried including the OAuth2 client id, secret, etc
None of these have worked. The error I'm getting in full is as follows:
Fatal error: Uncaught exception 'Google_ServiceException' with message 'Error calling GET https://www.googleapis.com/language/translate/v2/languages?key=XXXXXXXXXXXXXXXX: (403) Access Not Configured' in /home/google-api-php-client/src/io/Google_REST.php:66 Stack trace: #0 /home/google-api-php-client/src/io/Google_REST.php(36): Google_REST::decodeHttpResponse(Object(Google_HttpRequest)) #1 /home/google-api-php-client/src/service/Google_ServiceResource.php(186): Google_REST::execute(Object(Google_HttpRequest)) #2 /home/google-api-php-client/src/contrib/Google_TranslateService.php(39): Google_ServiceResource->__call('list', Array) #3 /home/google-api-php-client/examples/translate/simple.php(13): Google_LanguagesServiceResource->listLanguages() #4 {main} thrown in /home/google-api-php-client/src/io/Google_REST.php on line 66
I'm loosing the will to code with something that should be much more straight forward. Any advice to what might be the problem?
That 403 error is almost certainly related to the Simple API Key. If for example it was a usage restriction the message would say that so....
Double check and make sure you are using the Simple API key listed for server access and have it set to any IP. Specific IP should work but it may depend on how your server is set up.
You may even want to generate a new key just to make sure the one you have is valid. Specific IP would be best in the long run.
Just did the same thing working with the Custom Search API, had the browser key entered and it caused a big dent in the wall as I banged my head into it. . You Shouldn't need any of the OAuth settings just to get the basics working.
i had the same problem but with the analytics api. In my case i listed the ip of the server in the allowed ip addresses.
So when i removed all ip addresses from the google api's console allowed ip addresses then it started working.

api call in facebook is not working

Hi I m trying to create application in facebook but I m getting error as api call in facebook is not working properly. I m using following code:
try {
$uid = $facebook->getUser();
$fbme = $facebook->api('/me');
} catch (FacebookApiException $e) {
echo "uid: $uid<br>";
var_dump($fbme);
echo $e;
}
In this I get uid but $fbme is NULL. The error returned is CurlException: 6: name lookup timed out.
Why this is happening??
Pls help me.
It is interesting to note that the PHP
SDK will make the old REST API call to
different Facebook's server based on
whether the API call is a
"READ_ONLY_CALL" or not. For
"READ_ONLY_CALL" requests, they will
be passed to "api-read.facebook.com".
Otherwise, the request will be passed
to "api.facebook.com".
While I have no control on how the
name lookup is done on the web server
(as I am using web host services), I
have tried to amend facebook.php by
renaming "api-read.facebook.com" to
"api.facebook.com". Well, the name
lookup problem is resolved.
Looking at Facebook's bug tracking
system reveals the fact that I am not
the only one who have encountered
this. So, you may want to apply the
same "workaround" if you encounter the
same problem.
http://www.takwing.idv.hk/tech/fb_dev/phpsdk/learning_phpsdk_07.html
I had this same problem when developing locally on a virtual machine. I solved it by upping my Curl Connect Timeout.
Look for CURLOPT_CONNECTTIMEOUT = 10 in your facebook SDK. Try changing it to CURLOPT_CONNECTTIMEOUT = 30 or CURLOPT_CONNECTTIMEOUT = 60
Facebook has a continuous habit of changing its API methods and call techniques. Developers face a lot of problem for this. Recently they changed their version to 3.0.0. But for the above code, there is some error with your coding technique. You can match your code with this one and see what is wrong. as i have left Facebook programming, i can directly help you in the code. sorry.

Can't get "Live Delegated Authentication" to work

I try to get the Live Delegated Authentication to work for the purpose of reading the email addresses.
I am doing this in PHP with the help of the windowslivelogin library. The problem is that I get an error.
I'm not sure what I'm doing wrong, i registered my application on the Azure webpage and got the appid and the secret into the code. This is what i use to initialize the Live Library :
$o = new WindowsLiveLogin();
$o->setAppId('000000004801B670');
$o->setSecret('secret');
$o->setSecurityAlgorithm('wsignin1.0');
$o->setDebug(true);
$o->setPolicyUrl('http://www.google.com/aides.html');
$o->setReturnUrl("http://michaelp.dev.gamepoint.net/framework/mainsite/contactimporter/?service=live");
return $o;
Then I call
$this->LiveLibrary->getLoginUrl()
And after I Login in to Live, it posts 2 things back, $_POST['stoken'] and $_POST['action'].
As soon as I call
$this->LiveLibrary->processLogin($_REQUEST);
It fails and gives back an error that the token is invalid.
I tried getting Consent straight away by making redirecting to
$this->LiveLibrary->getConsentUrl("Contacts.View");
But that gives an 3007 error and says that it cant share the information
According to MS this means the following :
3007
Consent Service API failed in the <method name> method. The application verifier is invalid.
The offer security level requires that a valid application verifier be passed with the request.
I am using the following URL, generated by the library
https://consent.live.com/Delegation.aspx?ps=Contacts.Invite&ru=http%3A%2F%2Fmichaelp.dev.gamepoint.net%2Fframework%2Fmainsite%2Fcontactimporter%2F%3Fservice%3Dlive&pl=http%3A%2F%2Fwww.google.com%2Faides.html&app=appid%3D000000004801B670%26ts%3D1251722931%26sig%3DD2gkM%252F%252FwlRXXfS64NMrV%252Bkt50v6dAOcESblfRk7j%252FUE%253D
I don't understand most of the documentation Microsoft has on this thing, I think its really unclear and chaotic. Also the Sample I tried doesn't work. I get an error message, it can't validate/decode the token. Same I get when I try the processLogin().
Thanks in Advance,
Michael

Categories