Search code examples
androidgoogle-apishort-url

Urlshortener and 403 error on Android App


This is my first question on Stackoverflow and my brain is burned out due to all post I have read and all the tests I made.

At my app I would add the Short Url's service of Google. I had read this post, I have generated the API-KEY for Android Application in the Google API Console, put the key in my manifest, enable the URL Shortener API and set the quota per-user limit to 50 (in this moment I'm the only user of my app on a physical smartphone) but my method for short url give me this:

com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
{
"code": 403,
"errors": [
{
"domain": "usageLimits",
"message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.",
"reason": "dailyLimitExceededUnreg",
"extendedHelp": "https://code.google.com/apis/console"
}
],
"message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup."
}

This is the code of my method

import android.os.AsyncTask;
import android.util.Log;

import com.google.api.client.extensions.android.http.AndroidHttp;
import com.google.api.client.extensions.android.json.AndroidJsonFactory;
import com.google.api.services.urlshortener.Urlshortener;
import com.google.api.services.urlshortener.model.Url;

import java.io.IOException;
import java.util.concurrent.ExecutionException;

[...]

public String shortUrl (String url) {
    Log.d(TAG, "Start ShortUrl URL = " + url);
    //*** Gruppo AsyncTask ***//
    AsyncTask <String, Void, String> shortUrlEngine = new AsyncTask<String, Void, String>() {
        @Override
        protected void onPreExecute() {
            Log.d(TAG, "PreExecute");
        }

        @Override
        protected String doInBackground(String... params) {
            Log.d(TAG, "Start doInBackground");
            Log.d(TAG, "params[0] = " + params[0]);
            Urlshortener.Builder builder = new Urlshortener.Builder (AndroidHttp.newCompatibleTransport(), AndroidJsonFactory.getDefaultInstance(), null);
            builder.setApplicationName("it.kabadroid42.testurl");
            Urlshortener urlshortener = builder.build();

            com.google.api.services.urlshortener.model.Url url = new Url();
            url.setLongUrl(params[0]);
            try {
                url = urlshortener.url().insert(url).execute();
                Log.d(TAG, String.valueOf(url.getId()));
                return url.getId();
            } catch (IOException e) {
                Log.d(TAG, String.valueOf(e));
                return "Error (1)";
            }
        }

        @Override
        protected void onPostExecute(String s) {
            Log.d(TAG, "PostExecute");
        }
    };
    //*** Fine ***//
    String urlOut = "Error (2)";
    try {
         urlOut = shortUrlEngine.execute(url).get();
    } catch (InterruptedException e) {
        Log.d(TAG, "eccezione 1");
        e.printStackTrace();
    } catch (ExecutionException e) {
        Log.d(TAG, "eccezione 2");
        e.printStackTrace();
    }

    return urlOut;
}

I noticed that if I put my corret API-KEY or a fake in the manifest the result is the same. I think that my app dont send the API-KEY but I dont have idea why. For completeness this is part of my AndroidManifest.xml

<application ...>
  ...
<meta-data
        android:name="com.google.android.urlshortener.API_KEY"
        android:value="xxxx MY API KEY xxxx"/>
  ...
</application>

I have tried to move the meta-data in various position, change the api-key but the 403 error said me hello every time.

Thank You so much for any suggestion and sorry for my english, is not my native language but tuesday I start an english course :)


Solution

  • Was running into the same problem and solved with addition of setKey() as shown below. Use your Android key from the API console, not any Web key you may have used if you were using the old shortener via a web call.

        try {
                url = urlshortener.url().insert(url).setKey({your API Key}).execute();
                Log.d(TAG, String.valueOf(url.getId()));
                return url.getId();
            } catch (IOException e) {
                Log.d(TAG, String.valueOf(e));
                return "Error (1)";
            }