Problem/Motivation
Running a clean automated Drupal 7 installation without "google_fonts_api_key" set, the installation job triggers "The list of Google Fonts could not be fetched. Verify that your server can connect the Google Servers (https://www.googleapis.com)" marking the job as unsuccessful.
Steps to reproduce
Install google_fonts_api
Proposed resolution
Check for an environment variable with the google_fonts_api_key
| Comment | File | Size | Author |
|---|---|---|---|
| #20 | Restrict-and-rename-API-key-–-APIs-Services-–-LOM-map-–-Google-Cloud-Platform.png | 54.76 KB | hondaman900 |
| #8 | fontyourface-google-api-key-3183601-8.patch | 5.41 KB | bwaindwain |
| #2 | goolefont_key_as_env_var-3183601-2.patch | 888 bytes | apupiales |
Comments
Comment #2
apupiales commentedSuggested patch to solve the issue:
Comment #3
g-brodieiWe encountered the same issue on 9.0.8.
For others who have encountered the same issue, the current alternative is to get your own api key from google.
Comment #4
bwaindwain commentedIn Drupal 7, the API key can be set with drush.
drush vset google_fonts_api_key YOURAPIKEYToo bad the key is hard-coded in Drupal 8.
Comment #5
grahamshepherd commentedI have just started experiencing the same problem with 8.x-3.5 on drupal core 9.0.8. I had previously used fontyourface successfully on other sites with a nearly identical configuration.
Log reports:
GuzzleHttp\Exception\ClientException: Client error: `GET https://www.googleapis.com/webfonts/v1/webfonts?key=AIzaSyBgeqKlFdYj3Y7VwmrEXnXzpnx5TfKXG4o` resulted in a `400 Bad Request` response: { "error": { "code": 400, "message": "API key not valid. Please pass a valid API key.", "errors": [ (truncated...) in GuzzleHttp\Exception\RequestException::create() (line 113 of /home/.../public_html/.../vendor/guzzlehttp/guzzle/src/Exception/RequestException.php).I followed the method described in comment #3 above and the import of google fonts failed at the same point but with a slightly modified error:
GuzzleHttp\Exception\ClientException: Client error: `GET https://www.googleapis.com/webfonts/v1/webfonts?key=my_google_api_key` resulted in a `403 Forbidden` response: { "error": { "code": 403, "message": "Requests from referer \u003cempty\u003e are blocked.", "errors": [ (truncated...) in GuzzleHttp\Exception\RequestException::create() (line 113 of /home/.../public_html/.../vendor/guzzlehttp/guzzle/src/Exception/RequestException.php).Comment #6
g-brodieiHi @GrahamShepherd, maybe it's due to the Application restrictions on your google api key? I encountered similar issues as well. Once the config is set correctly and I waited 5~ min to take effect, it works on both of my local dev and staging site smoothly.
Comment #7
grahamshepherd commentedThanks, g-brodiei.
I tried that. Firstly no API restrictions and then I added "Web Fonts Developer API" restriction. Same result. Removed all API restrictions.
Same result. I have waited a couple of days now and repeated the exercise.
The error is no longer "API key not valid." but "Requests from referer \u003cempty\u003e are blocked." This is probably the clue but I can't decipher it.
Comment #8
bwaindwain commentedHere's a patch that adds an api key field just like typekit and fonts.com.
Comment #9
bwaindwain commentedComment #10
leeksoup commentedI have the same problem with D 8.9.11.
I tried the fix in #3, but it fails silently. I can paste the $uri into a browser and it pulls the correct info but doesn't work from the module, and also I'm not seeing any new errors in the logs.
Wups -- user error. It is working now. Would still love to have @bwaindwain's patch rolled into the codebase to allow setting the API key without having to edit the module file. Thanks.
Comment #11
mav_fly commentedI did the patch from #8, and then you get an api key field where you can paste your google api key. After doing that you can import the google fonts without any problem.
Thanks bwaindwain
Comment #12
Hooligan commentedThank you
#8 and an API Key saved me.
Comment #13
extect commented#8 perfectly solves the issue, both on D8 and D9.
Comment #15
neslee canil pintoCommited to dev branch. Thanks.
Comment #17
sclsweb commentedI see this is closed, but I'm posting this for anyone else (future me?) running into this with D7. The D7 patch did not work for me. It seems to apply cleanly against 7.x-2.8, but does not help with the original issue. Clicking "Import Google fonts" just triggers the same error message, and "Notice: Undefined index: GOOGLE_FONTS_API_KEY in google_fonts_api_fontyourface_import()." With detailed logging turned on, it also gave the following message:
drupal_http_request response: stdClass Object ( [request] => GET /webfonts/v1/webfonts?key=AIzaSyBgeqKlFdYj3Y7VwmrEXnXzpnx5TfKXG4o HTTP/1.0 User-Agent: Drupal (+http://drupal.org/) Host: www.googleapis.com [data] => { "error": { "code": 400, "message": "API key not valid. Please pass a valid API key.", "errors": [ { "message": "API key not valid. Please pass a valid API key.", "domain": "global", "reason": "badRequest" } ], "status": "INVALID_ARGUMENT" } } [protocol] => HTTP/1.0 [status_message] => Bad Request [headers] => Array ( [vary] => Referer [content-type] => application/json; charset=UTF-8 [date] => Thu, 18 Feb 2021 03:46:56 GMT [server] => ESF [content-length] => 305 [x-xss-protection] => 0 [x-frame-options] => SAMEORIGIN [x-content-type-options] => nosniff [alt-svc] => h3-29=":443"; ma=2592000,h3-T051=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43" ) [code] => 400 [error] => Bad Request )
I got the same error trying to import Google fonts after setting the API key with drush (using the same key from the patch):
drush vset google_fonts_api_key AIzaSyBgeqKlFdYj3Y7VwmrEXnXzpnx5TfKXG4oI was able to work around it by getting my own API key and setting it with drush.
Does anyone know if it's better to use a separate API key for each site, or whether it's OK to reuse the same key for several different projects (like a multisite where several sites use Google fonts)?
Comment #18
hondaman900 commentedI know this is closed, but I'm still getting this issue with current D8 (8.9.13) and current @font-your-face (8.x-3.6) and entering a fresh Google Web Fonts Developer API enabled. By all accounts in this issue thread this should be fine and working, but I'm getting the following error:
The list of Google Fonts could not be fetched. Verify that your server can connect to the Google servers (https://www.googleapis.com). Error: Client error: `GET https://www.googleapis.com/webfonts/v1/webfonts?key={my Google API Key here}` resulted in a `403 Forbidden` response: { "error": { "code": 403, "message": "Requests from referer \u003cempty\u003e are blocked.", "errors": [ (truncated...)The log shows:
TypeError: Argument 1 passed to _google_fonts_api_convert_api_results() must be of the type array, bool given, called in /home/mywebsite.com/modules/fontyourface/modules/google_fonts_api/google_fonts_api.module on line 116 in _google_fonts_api_convert_api_results() (line 183 of /home/mywebsite.com/modules/fontyourface/modules/google_fonts_api/google_fonts_api.module)Any suggestions about where to go from here? Please re-open this issue as it appears it is not yet resolved.
Comment #19
bwaindwain commentedWe are not seeing this error. Have you enabled any API key restrictions? Maybe check that your Google API key restrictions match your actual usage.
Comment #20
hondaman900 commented@bwaindwain Yes, I had HTTP referral restrictions, but using the same API key for Google Maps on the same site with those restrictions works fine. Removing the restrictions seems to have resolved the issue, but now I have an exposed API key, which is not good nor recommended.
Do we know what a referring URL for a website using @font-your-face would look like? If so we could add that to the list of whitelisted URL's under that restriction and retain the protection.
Comment #21
tedwyer commentedTry regenerating your key on google dev. Worked for me.
Comment #22
bcobin commented@bwaindwain - Thanks so much for #4. I was tearing my hair out. Glad I found this thread!
I'm on D7 so I would have missed this as I'm sure are many who've run into the issue. To clarify, it looks like Google Fonts will no longer work without an API key, correct? (Just as they've done with everything else.)
I'd ask if it would be possible to cross-post at least some of this to D7, even though the D7 version is no longer supported. Thanks again!