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

Comments

apupiales created an issue. See original summary.

apupiales’s picture

StatusFileSize
new888 bytes

Suggested patch to solve the issue:

g-brodiei’s picture

Version: 7.x-2.x-dev » 8.x-3.x-dev

We 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.

  1. https://developers.google.com/fonts/docs/developer_api
  2. https://console.developers.google.com/apis/api/webfonts.googleapis.com
  3. Follow the steps on https://help.touchsize.com/knowledgebase/how-to-set-your-own-google-font....

1. Go to https://console.developers.google.com/projectselector/apis/credentials
2. Select a project or start a new project
3. On the left side of the screen go to ‘Credentials’ tab and choose ‘API key’, then choose ‘Browser key’, give it a name and press ‘Create’.
4. Copy the given API key
5. From the left side of the screen go to ‘Overview’, in the ‘Other popular APIs’ section choose the ‘Web Fonts Developer API’.
6. Press ‘Enable API’.
- https://help.touchsize.com/knowledgebase/how-to-set-your-own-google-font...

  1. Swap the api key in google_fonts_api.module line 150 (as of ver.8.x-3.5)
  2. Refresh cache, wait 5 min for google's API to take effect
  3. Rerun Import from google_fonts_api on /admin/appearance/font/settings.
  4. All set
//Swap the key of $uri in google_fonts_api.module line 150.
  try {
    $uri = 'https://www.googleapis.com/webfonts/v1/webfonts?key=AIzaSyBgeqKlFdYj3Y7VwmrEXnXzpnx5TfKXG4o';
    $response = \Drupal::httpClient()->get($uri, ['headers' => ['Accept' => 'text/plain'], 'verify' => FALSE]);
    $data = (string) $response->getBody();
  }
bwaindwain’s picture

In Drupal 7, the API key can be set with drush.

drush vset google_fonts_api_key YOURAPIKEY

Too bad the key is hard-coded in Drupal 8.

grahamshepherd’s picture

I 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).

g-brodiei’s picture

Hi @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.

grahamshepherd’s picture

Thanks, 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.

bwaindwain’s picture

Here's a patch that adds an api key field just like typekit and fonts.com.

bwaindwain’s picture

Status: Active » Needs review
leeksoup’s picture

I 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.

mav_fly’s picture

I 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

Hooligan’s picture

Thank you
#8 and an API Key saved me.

extect’s picture

Status: Needs review » Reviewed & tested by the community

#8 perfectly solves the issue, both on D8 and D9.

neslee canil pinto’s picture

Status: Reviewed & tested by the community » Fixed

Commited to dev branch. Thanks.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

sclsweb’s picture

I 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 AIzaSyBgeqKlFdYj3Y7VwmrEXnXzpnx5TfKXG4o

I 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)?

hondaman900’s picture

I 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.

bwaindwain’s picture

We are not seeing this error. Have you enabled any API key restrictions? Maybe check that your Google API key restrictions match your actual usage.

hondaman900’s picture

@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.

Google API restrictions

tedwyer’s picture

Try regenerating your key on google dev. Worked for me.

bcobin’s picture

@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!