2

I am trying to use a URL shortener, with Scribe, based on this example.

However, I want to make sure I can track the visits which my short URL gets, which means it must be unique. To create unique links, I need to authenticate with Google, as per this.

Signed in

Your links are automatically added to goo.gl where you can track their use.

A unique short URL is created each time a long URL is shortened.

Signed out

Your links won’t show up on your goo.gl page.

The same short URL is reused each time a long URL is shortened by you or someone else.

In the example, the oAthRequest is not signed with the oAuthService. I have updated this so that it can sign the request and send it (as a signed in user).

Here is my code:

private static final String API_KEY = "XXXXXXXX";
private static final String API_URL = "https://www.googleapis.com/urlshortener/v1/url";

private static final String API_URL_WITH_KEY = API_URL + "?key=" + API_KEY;

public TrackableLink createTrackableLink(String longUrl) {
    OAuthService oAuthService = new ServiceBuilder()

    //Google Api Provider - Google's URL Shortener API is part of Google Platform APIs
    .provider(GoogleApi.class)

    /*
        Using "anonymous" as API Key & Secret because Google's URL Shortener service
        does not necessarily requires App identification and/or User Information Access
     */
    .apiKey("anonymous")
    .apiSecret("anyonymous")
    //OAuth 2.0 scope for the Google URL Shortener API
    .scope("https://www.googleapis.com/auth/urlshortener")

    //build it!
    .build();

    Token requestToken = oAuthService.getRequestToken();

    OAuthRequest request = new OAuthRequest(Verb.POST, API_URL_WITH_KEY);
    request.addHeader("Content-Type", "application/json");
    request.addPayload(new JSONObject().put(RESPONSE_LONG_URL, longUrl)
            .toString());

    oAuthService.signRequest(requestToken, request);

    Response response = request.send();
    JSONObject json = new JSONObject(response.getBody());
    String shortUrl = json.getString(RESPONSE_SHORT_URL);
    TrackableLink tl = new TrackableLink(longUrl, shortUrl);

    return tl;
}

I replace the "anonymous" details with my values from the Google API website, and I get this exception:

Can't extract token and secret from this: 'Consumer is not registered: 7629638329XXXXXXXXXXX.apps.googleusercontent.com

I'm not sure exactly what I am doing wrong here. I have tried almost every combination of values for the key/secret from the various keys the google console gives me, could this be caused by something else other than something up with the API key?

Any ideas why I am getting the consumer is not registered error? On my Google account, I have enabled the API.

Community
  • 1
  • 1
ThePerson
  • 3,048
  • 8
  • 43
  • 69

0 Answers0