我在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字符串中添加服务器密钥,但我没有看到问题。有任何想法吗?
答案 0 :(得分:4)
您正在将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' );