我正在尝试基于此示例使用带有 Scribe 的 URL 缩短器。
但是,我想确保我可以跟踪我的短 URL 获得的访问,这意味着它必须是唯一的。要创建唯一链接,我需要根据此向 Google 进行身份验证。
已登录
您的链接会自动添加到 goo.gl,您可以在其中跟踪它们的使用情况。
每次缩短长 URL 时都会创建一个唯一的短 URL。
退出
您的链接不会显示在您的 goo.gl 页面上。
每次您或其他人缩短长 URL 时,都会重复使用相同的短 URL。
在示例中,oAthRequest
未使用 签名oAuthService
。我已对此进行了更新,以便它可以签署请求并将其发送(作为登录用户)。
这是我的代码:
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;
}
我用我在 Google API 网站上的值替换了“匿名”详细信息,我得到了这个例外:
无法从中提取令牌和秘密:'消费者未注册:7629638329XXXXXXXXXXX.apps.googleusercontent.com
我不确定我在这里做错了什么。我已经尝试了谷歌控制台给我的各种密钥的密钥/秘密值的几乎所有组合,这可能是由 API 密钥以外的其他原因引起的吗?
任何想法为什么我让消费者没有注册错误?在我的 Google 帐户上,我启用了 API。