use of org.scribe.builder.ServiceBuilder in project openhab1-addons by openhab.
the class OpenPathsBinding method getUserLocation.
@SuppressWarnings("unchecked")
private Location getUserLocation(String accessKey, String secretKey) {
// build the OAuth service using the access/secret keys
OAuthService service = new ServiceBuilder().provider(new OpenPathsApi()).apiKey(accessKey).apiSecret(secretKey).build();
// build the request
OAuthRequest request = new OAuthRequest(Verb.GET, "https://openpaths.cc/api/1");
service.signRequest(Token.empty(), request);
request.addQuerystringParameter("num_points", "1");
// send the request and check we got a successful response
Response response = request.send();
if (!response.isSuccessful()) {
logger.error("Failed to request the OpenPaths location, response code: " + response.getCode());
return null;
}
// parse the response to build our location object
Map<String, Object> locationData;
String toParse = "{}";
try {
ObjectMapper jsonReader = new ObjectMapper();
toParse = response.getBody();
toParse = toParse.substring(1, toParse.length() - 2);
locationData = jsonReader.readValue(toParse, Map.class);
} catch (JsonParseException e) {
logger.error("Error parsing JSON:\n" + toParse, e);
return null;
} catch (JsonMappingException e) {
logger.error("Error mapping JSON:\n" + toParse, e);
return null;
} catch (IOException e) {
logger.error("An I/O error occured while decoding JSON:\n" + response.getBody());
return null;
}
float latitude = Float.parseFloat(locationData.get("lat").toString());
float longitude = Float.parseFloat(locationData.get("lon").toString());
String device = locationData.get("device").toString();
return new Location(latitude, longitude, device);
}
use of org.scribe.builder.ServiceBuilder in project camel by apache.
the class YammerAccessCodeGenerator method main.
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Paste the consumerKey here");
System.out.print(">>");
String apiKey = in.nextLine();
System.out.println("Paste the consumerSecret here");
System.out.print(">>");
String apiSecret = in.nextLine();
OAuthService service = new ServiceBuilder().provider(YammerApi.class).apiKey(apiKey).apiSecret(apiSecret).build();
String authorizationUrl = service.getAuthorizationUrl(EMPTY_TOKEN);
System.out.println("Go and authorize your app here (eg. in a web browser):");
System.out.println(authorizationUrl);
System.out.println("... and paste the authorization code here");
System.out.print(">>");
Verifier verifier = new Verifier(in.nextLine());
System.out.println();
Token accessToken = service.getAccessToken(EMPTY_TOKEN, verifier);
System.out.println("Your Access Token is: " + accessToken);
System.out.println();
in.close();
}
use of org.scribe.builder.ServiceBuilder in project Notes by lguipeng.
the class EvernoteOAuthHelper method createOAuthService.
protected static OAuthService createOAuthService(BootstrapProfile bootstrapProfile, String consumerKey, String consumerSecret) {
String host = bootstrapProfile.getSettings().getServiceHost();
if (host == null) {
return null;
}
Uri uri = new Uri.Builder().authority(host).scheme("https").build();
Class<? extends Api> apiClass;
switch(uri.toString()) {
case EvernoteSession.HOST_SANDBOX:
apiClass = EvernoteApi.Sandbox.class;
break;
case EvernoteSession.HOST_PRODUCTION:
apiClass = EvernoteApi.class;
break;
case EvernoteSession.HOST_CHINA:
apiClass = YinxiangApi.class;
break;
default:
throw new IllegalArgumentException("Unsupported Evernote host: " + host);
}
return new ServiceBuilder().provider(apiClass).apiKey(consumerKey).apiSecret(consumerSecret).callback(CALLBACK_SCHEME + "://callback").build();
}
use of org.scribe.builder.ServiceBuilder in project jaggery by wso2.
the class OAuthHostObject method jsConstructor.
/**
* var provider = {
* "oauth_version" : "1",
* "authorization_url" : "https://www.linkedin.com/uas/oauth/authorize",
* "access_token_url" : "https://api.linkedin.com/uas/oauth/accessToken",
* "request_token_url" : "https://api.linkedin.com/uas/oauth/requestToken",
* "api_key" : "key",
* "api_secret" : "secret"
* }
* new OAuthProvider(provider);
*/
public static Scriptable jsConstructor(Context cx, Object[] args, Function ctorObj, boolean inNewExpr) throws ScriptException {
OAuthHostObject oauthho = new OAuthHostObject();
if (args.length == 1) {
if (!(args[0] == Context.getUndefinedValue()) && args[0] instanceof NativeObject) {
NativeObject config = (NativeObject) args[0];
Gson gson = new Gson();
ProviderConfig providerConfig = gson.fromJson(HostObjectUtil.serializeJSON(config), ProviderConfig.class);
if (providerConfig.getApi_key() == null || providerConfig.getApi_secret() == null || providerConfig.getAccess_token_url() == null || providerConfig.getAuthorization_url() == null || providerConfig.getOAuth_version() == null) {
throw new ScriptException("API configuration not specified");
}
oauthho.apiKey = providerConfig.getApi_key();
oauthho.apiSecret = providerConfig.getApi_secret();
if (providerConfig.getOAuth_version() == 1.0) {
if (providerConfig.getRequest_token_url() == null) {
throw new ScriptException("API configuration not specified");
}
oauthho.oAuthVersion = OAuthVersion.OAUTH1;
GenericOAuth10aApi oauth10aApi = new GenericOAuth10aApi();
oauth10aApi.setAccessTokenEndpoint(providerConfig.getAccess_token_url());
oauth10aApi.setAuthorizationUrl(providerConfig.getAuthorization_url());
oauth10aApi.setRequestTokenEndpoint(providerConfig.getRequest_token_url());
oauthho.oauthService = new ServiceBuilder().provider(oauth10aApi).apiKey(oauthho.apiKey).apiSecret(oauthho.apiSecret).build();
} else if (providerConfig.getOAuth_version() == 2.0) {
if (providerConfig.getCallback_url() == null) {
throw new ScriptException("API configuration not specified");
}
oauthho.oAuthVersion = OAuthVersion.OAUTH2;
GenericOAuth20Api oauth20Api = new GenericOAuth20Api();
oauth20Api.setAccessTokenEP(providerConfig.getAccess_token_url());
oauth20Api.setAuthorizeUrl(providerConfig.getAuthorization_url());
oauthho.oauthService = new ServiceBuilder().provider(oauth20Api).apiKey(oauthho.apiKey).apiSecret(oauthho.apiSecret).build();
}
}
return oauthho;
} else {
throw new ScriptException("API configuration not specified");
}
}
use of org.scribe.builder.ServiceBuilder in project sling by apache.
the class XingOauthAuthenticationHandler method configure.
protected synchronized void configure(final ComponentContext componentContext) {
final Dictionary properties = componentContext.getProperties();
consumerKey = PropertiesUtil.toString(properties.get(CONSUMER_KEY_PARAMETER), "").trim();
consumerSecret = PropertiesUtil.toString(properties.get(CONSUMER_SECRET_PARAMETER), "").trim();
callbackUrl = PropertiesUtil.toString(properties.get(CALLBACK_URL_PARAMETER), "").trim();
usersMeUrl = PropertiesUtil.toString(properties.get(USERS_ME_URL_PARAMETER), DEFAULT_USERS_ME_URL).trim();
if (StringUtils.isEmpty(consumerKey)) {
logger.warn("configured consumer key is empty");
}
if (StringUtils.isEmpty(consumerSecret)) {
logger.warn("configured consumer secret is empty");
}
if (StringUtils.isEmpty(callbackUrl)) {
logger.warn("configured callback URL is empty");
}
if (StringUtils.isEmpty(usersMeUrl)) {
logger.warn("configured users me URL is empty");
}
if (!StringUtils.isEmpty(consumerKey) && !StringUtils.isEmpty(consumerSecret) && !StringUtils.isEmpty(callbackUrl)) {
oAuthService = new ServiceBuilder().provider(XingApi.class).apiKey(consumerKey).apiSecret(consumerSecret).callback(callbackUrl).build();
} else {
oAuthService = null;
}
logger.info("configured with consumer key '{}', callback url '{}' and users me url '{}'", consumerKey, callbackUrl, usersMeUrl);
}
Aggregations