简体   繁体   English

使用谷歌API密钥的URL Shortener

[英]URL Shortener using google API key

I am using this code to shorten my long Url. 我正在使用此代码来缩短我的长网址。 I am doing something wrong? 我做错了什么?

Urlshortener.Builder builder = new Urlshortener.Builder (AndroidHttp.newCompatibleTransport(),
        AndroidJsonFactory.getDefaultInstance(), null);
Urlshortener urlshortener = builder.build();

com.google.api.services.urlshortener.model.Url url = new com.google.api.services.urlshortener.model.Url();
url.setLongUrl(longUrl);
try {
    Urlshortener.Url.Insert insert=urlshortener.url().insert(url);
    insert.setKey(key);
    url = insert.execute();
    return url.getId();
} catch (IOException e) {
    return null;
}

But it gives me Exception. 但它给了我例外。

com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
{
"code": 403,
"errors": [
{
  "domain": "usageLimits",
  "message": "The Android package name and signing-certificate fingerprint, null and null, do not match the app restrictions configured on your API key. Please use the API Console to update your key restrictions.",
  "reason": "ipRefererBlocked",
  "extendedHelp": "https://console.developers.google.com/apis/credentials?project=796104768654"
}
],
"message": "The Android package name and signing-certificate fingerprint, null and null, do not match the app restrictions configured on your API key. Please use the API Console to update your key restrictions."
}

Looks like you have packaged the app in development(unsigned). 看起来您已将应用程序打包在开发中(未签名)。 To use this api in android, you have to, in the API console, register the key that your android app will be signed with. 要在Android中使用此api,您必须在API控制台中注册您的Android应用程序将使用的密钥。

From Google:: If your application accesses APIs directly from Android (as opposed to from a web server), then to register your digitally signed .apk file's public certificate in the console, you need your Android package name and SHA-1 fingerprint. 来自Google ::如果您的应用程序直接从Android访问API(而不是从Web服务器访问),那么要在控制台中注册数字签名的.apk文件的公共证书,您需要Android包名称和SHA-1指纹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM