Google API dailyLimitExceededUnreg

Google API dailyLimitExceededUnreg

我在 PHP 应用程序中使用 Google URL 缩短器 API。它已经工作了几个月,但现在我收到了这个错误:

[errors] => Array (
   [0] => stdClass Object (
      [domain] => usageLimits
      [reason] => dailyLimitExceededUnreg
      [message] => Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.
      [extendedHelp] => https://code.google.com/apis/console
    )
)
[code] => 403
[message] => Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.

我正在使用来自开发者控制台的服务器密钥,我重新生成了密钥,甚至删除了服务器访问密钥部分并重新添加它,但我一直收到相同的身份验证错误。

$query_array = json_encode( array( "longUrl" => $data['long_url'], 'key' => 'AIza-Key' ) ); 

$curl = curl_init( 'https://www.googleapis.com/urlshortener/v1/url' );

curl_setopt($curl, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json' ) );

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

curl_setopt($curl, CURLOPT_POST, 1);

curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query( $query_array ) ) ;             

curl_setopt($curl, CURLINFO_HEADER_OUT, true);

$short_url = json_decode( curl_exec( $curl ) );

$header = curl_getinfo($curl, CURLINFO_HEADER_OUT );

curl_close( $curl );

print_r( $short_url ) ; // shows above error

这个 API 不应该那么难,只需在 URL 字符串中添加服务器密钥,但我没有看到问题。有任何想法吗?

您正在将 key 添加到 POST 正文而不是作为 URL 查询字符串。将代码更改为:

$query_array = json_encode( array( "longUrl" => $data['long_url'] ) ); 

$curl = curl_init( 'https://www.googleapis.com/urlshortener/v1/url?key=AIza-Key' );