简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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