我正在尝试使用基于this example的Scribe的URL缩短器。
但是,我想确保我可以跟踪短网址的访问次数,这意味着它必须是唯一的。要创建唯一的链接,我需要根据this对Google进行身份验证。
登录
您的链接会自动添加到goo.gl,您可以在其中跟踪其使用情况。
每次缩短长网址时都会创建一个唯一的短网址。
退出
您的链接不会显示在goo.gl页面上。
每次您或其他人缩短长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。