132 lines
3.8 KiB
Java
132 lines
3.8 KiB
Java
/**
|
|
* Copyright (C) 2017-2018 Christian Pierre MOMON <cmomon@april.org>
|
|
* Copyright (C) 2011-2013 Nicolas Vinot <aeris@imirhil.fr>
|
|
*
|
|
* This file is part of (April) Hebdobot.
|
|
*
|
|
* Hebdobot is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* Hebdobot is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Affero General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with Hebdobot. If not, see <http://www.gnu.org/licenses/>
|
|
*/
|
|
package org.april.hebdobot.identica;
|
|
|
|
import java.util.Scanner;
|
|
|
|
import org.scribe.builder.ServiceBuilder;
|
|
import org.scribe.builder.api.Api;
|
|
import org.scribe.model.OAuthRequest;
|
|
import org.scribe.model.Token;
|
|
import org.scribe.model.Verb;
|
|
import org.scribe.model.Verifier;
|
|
import org.scribe.oauth.OAuthService;
|
|
|
|
/**
|
|
* The Class StatusNetClient.
|
|
*/
|
|
public abstract class StatusNetClient
|
|
{
|
|
private final OAuthService service;
|
|
private final Token token;
|
|
|
|
/**
|
|
* Instantiates a new status net client.
|
|
*
|
|
* @param apiClass
|
|
* the api class
|
|
* @param apiKey
|
|
* the api key
|
|
* @param apiSecret
|
|
* the api secret
|
|
* @param tokenKey
|
|
* the token key
|
|
* @param tokenSecret
|
|
* the token secret
|
|
*/
|
|
public StatusNetClient(final Class<? extends Api> apiClass, final String apiKey, final String apiSecret, final String tokenKey,
|
|
final String tokenSecret)
|
|
{
|
|
this.service = getService(apiClass, apiKey, apiSecret);
|
|
this.token = new Token(tokenKey, tokenSecret);
|
|
}
|
|
|
|
/**
|
|
* Gets the api url.
|
|
*
|
|
* @return the api url
|
|
*/
|
|
protected abstract String getApiUrl();
|
|
|
|
/**
|
|
* Post.
|
|
*
|
|
* @param message
|
|
* the message
|
|
*/
|
|
public void post(final String message)
|
|
{
|
|
final OAuthRequest request = new OAuthRequest(Verb.POST, this.getApiUrl() + "/statuses/update.json");
|
|
request.addBodyParameter("status", message);
|
|
this.service.signRequest(this.token, request);
|
|
request.send();
|
|
}
|
|
|
|
/**
|
|
* Gets the service.
|
|
*
|
|
* @param apiClass
|
|
* the api class
|
|
* @param apiKey
|
|
* the api key
|
|
* @param apiSecret
|
|
* the api secret
|
|
* @return the service
|
|
*/
|
|
private static OAuthService getService(final Class<? extends Api> apiClass, final String apiKey, final String apiSecret)
|
|
{
|
|
OAuthService result;
|
|
|
|
result = new ServiceBuilder().provider(apiClass).apiKey(apiKey).apiSecret(apiSecret).build();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Register.
|
|
*
|
|
* @param apiClass
|
|
* the api class
|
|
* @param apiKey
|
|
* the api key
|
|
* @param apiSecret
|
|
* the api secret
|
|
*/
|
|
public static void register(final Class<? extends Api> apiClass, final String apiKey, final String apiSecret)
|
|
{
|
|
final OAuthService service = getService(apiClass, apiKey, apiSecret);
|
|
final Token requestToken = service.getRequestToken();
|
|
System.out.println(service.getAuthorizationUrl(requestToken));
|
|
System.out.print(">>");
|
|
final Scanner scanner = new Scanner(System.in);
|
|
try
|
|
{
|
|
final Verifier verifier = new Verifier(scanner.nextLine());
|
|
final Token token = service.getAccessToken(requestToken, verifier);
|
|
System.out.println(token);
|
|
}
|
|
finally
|
|
{
|
|
scanner.close();
|
|
}
|
|
}
|
|
}
|