SAS token - Signature fields not well formed - java

I want to generate a SAS token for access to my blob container where are some of my media files.
So I created a class SharedAccessSignature.java with this code:
public class SharedAccessSignature
{
private final String signature;
private final String signedPermission;
private final String signedStart;
private final String signedExpiry;
private final String signedIdentifier;
private final String signedIp;
private final String signedProtocol;
private final String signedVersion;
private final String signedResource;
private SharedAccessSignature(SasBuilder builder)
{
signedPermission = formatAsUrlParameter("sp", builder.signedPermission);
signedStart = formatAsUrlParameter("st", builder.signedStart);
signedExpiry = formatAsUrlParameter("se", builder.signedExpiry);
signedIdentifier = formatAsUrlParameter("si", builder.signedIdentifier);
signedIp = formatAsUrlParameter("sip", builder.signedIp);
signedProtocol = formatAsUrlParameter("spr", builder.signedProtocol);
signedVersion = formatAsUrlParameter("sv", builder.signedVersion);
signedResource = formatAsUrlParameter("sr", builder.signedResource);
signature = "sig=" + new SasBuilder().encodeUtf8(builder.signature);
}
private String formatAsUrlParameter(String parameterKey, String parameterValue)
{
if (StringUtils.isNotBlank(parameterValue))
{
return parameterKey + "=" + parameterValue + "&";
}
return "";
}
#Override
public String toString()
{
return new StringBuilder()
.append(signedVersion)
.append(signedResource)
.append(signedStart)
.append(signedExpiry)
.append(signedPermission)
.append(signedIp)
.append(signedProtocol)
.append(signedIdentifier)
.append(signature)
.toString();
}
public static class SasBuilder
{
private String signature = "";
private String signedPermission = "";
private String signedStart = "";
private String signedExpiry = "";
private String canonicalizedResource = "";
private String signedIdentifier = "";
private String signedIp = "";
private String signedProtocol = "";
private String signedVersion = "";
private String signedResource = "";
public SasBuilder signedVersion(String signedVersion)
{
this.signedVersion = signedVersion;
return this;
}
public SasBuilder signedPermission(String signedPermission)
{
this.signedPermission = signedPermission;
return this;
}
public SasBuilder canonicalizedResource(String canonicalizedResource)
{
this.canonicalizedResource = canonicalizedResource;
return this;
}
public SasBuilder signedIp(String signedIp)
{
this.signedIp = signedIp;
return this;
}
public SasBuilder signedProtocol(String signedProtocol)
{
this.signedProtocol = signedProtocol;
return this;
}
public SasBuilder signedIdentifier(String signedIdentifier)
{
this.signedIdentifier = signedIdentifier;
return this;
}
public SasBuilder signedExpiry(String signedExpiry)
{
this.signedExpiry = signedExpiry;
return this;
}
public SasBuilder signedStart(String signedStart)
{
this.signedStart = signedStart;
return this;
}
public SasBuilder signedResource(String signedResource)
{
this.signedResource = signedResource;
return this;
}
public SharedAccessSignature build()
{
String toBeAsEnvironmentVariable_securityKey = "....";
signature = generateSasSignature(toBeAsEnvironmentVariable_securityKey, stringToSign());
checkPreconditions();
return new SharedAccessSignature(this);
}
private String generateSasSignature(String key, String input)
{
SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(), "HmacSHA256");
Encoder encoder = Base64.getEncoder();
Mac sha256_HMAC = null;
String hash = null;
try
{
sha256_HMAC = Mac.getInstance("HmacSHA256");
sha256_HMAC.init(secret_key);
hash = new String(encoder.encode(sha256_HMAC.doFinal(input.getBytes("UTF-8"))));
}
catch (InvalidKeyException | NoSuchAlgorithmException | IllegalStateException | UnsupportedEncodingException e)
{
e.printStackTrace();
}
return hash;
}
private String stringToSign()
{
StringBuilder strToSign = new StringBuilder();
strToSign.append(signedPermission).append("\n");
strToSign.append(signedStart).append("\n");
strToSign.append(signedExpiry).append("\n");
strToSign.append(canonicalizedResource).append("\n");
strToSign.append(signedIdentifier).append("\n");
strToSign.append(signedIp).append("\n");
strToSign.append(signedProtocol).append("\n");
strToSign.append(signedVersion).append("\n");
strToSign.append("").append("\n");
strToSign.append("").append("\n");
strToSign.append("").append("\n");
strToSign.append("").append("\n");
strToSign.append("");
return strToSign.toString();
}
private void checkPreconditions()
{
if (StringUtils.isBlank(signedVersion) || StringUtils.isBlank(signedResource) || StringUtils.isBlank(signedPermission) || StringUtils.isBlank(signedExpiry) || StringUtils.isBlank(signature))
{
throw new IllegalStateException("SAS Builder: SignedVersion, signedResource, SignedPermission, SignedExpiry, Signature must be set.");
}
}
private String encodeUtf8(String textToBeEncoded)
{
try
{
return URLEncoder.encode(textToBeEncoded, "UTF-8");
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
return textToBeEncoded;
}
}
}
And then I try to generate a SAS token like this:
SharedAccessSignature s = new SharedAccessSignature.SasBuilder()
.signedPermission("rwd")
.signedStart("2018-01-31T10:48:41Z")
.signedExpiry("2018-04-06T18:48:41Z")
.signedVersion("2015-04-05")
.signedResource("b")
.canonicalizedResource("/blob/myaccount")
.signedProtocol("https")
.build();
outcome:
sv=2015-04-05&sr=b&st=2018-01-31T10:48:41Z&se=2018-04-06T18:48:41Z&sp=rwd&spr=https&sig=kd09Y%2FTL5V%2F570VWRuEfq7XbEHvcgo4Z%2F2y9t4OswY8%3D
GET request:
https://account.blob.core.cloudapi.de/container/filename.mp4?sv=2015-04-05&sr=b&st=2018-01-31T10:48:41Z&se=2018-04-06T18:48:41Z&sp=rwd&spr=https&sig=kd09Y%2FTL5V%2F570VWRuEfq7XbEHvcgo4Z%2F2y9t4OswY8%3D
But as I am sending that request with this generated token there commes this Error from azure:
<Error>
<Code>AuthenticationFailed</Code>
<Message>
Server failed to authenticate the request. Make sure the value of
Authorization header is formed correctly including the signature.
</Message>
<AuthenticationErrorDetail>
Signature did not match. String to sign used was rwd 2018-01-31T10:48:41Z
2018-04-06T18:48:41Z /blob/globalweb/..... https 2015-04-05
</AuthenticationErrorDetail>
</Error>
EDIT:
I am desperate... I donĀ“t understand it... What is wrong on this "string-to-sign"? Why the "Signature did not match"?
--------
rwd\n
2018-01-31T10:48:41Z\n
2018-04-06T18:48:41Z\n
/blob/globalweb/videos-martindale\n
\n
\n
https\n
2015-04-05\n
\n
\n
\n
\n
-------
//link: https://globalweb.blob.core.cloudapi.de/videos-martindale/somevideo.mp4?sv=2015-04-05&sr=c&st=2018-01-31T10:48:41Z&se=2018-04-06T18:48:41Z&sp=rwd&spr=https&sig=kd09Y%2FTL5V%2F570VWRuEfq7XbEHvcgo4Z%2F2y9t4OswY8%3D
<Error>
<Code>AuthenticationFailed</Code>
<Message>
Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature. RequestId:644e47a6-001e-0050-3f20-abc0f0000000 Time:2018-02-21T14:31:10.9429817Z
</Message>
<AuthenticationErrorDetail>
Signature did not match. String to sign used was rwd 2018-01-31T10:48:41Z 2018-04-06T18:48:41Z /blob/globalweb/videos-martindale https 2015-04-05
</AuthenticationErrorDetail>
</Error>

The main problem is on you generateSasSignature method. It should decode the key from Base64. Like the following:
public static String generateSasSignature(String key, String input) {
SecretKeySpec secret_key = new SecretKeySpec(Base64.getDecoder().decode(key), "HmacSHA256");
Encoder encoder = Base64.getEncoder();
Mac sha256_HMAC = null;
String hash = null;
try {
sha256_HMAC = Mac.getInstance("HmacSHA256");
sha256_HMAC.init(secret_key);
hash = new String(encoder.encode(sha256_HMAC.doFinal(input.getBytes("UTF-8"))));
}
catch (InvalidKeyException | NoSuchAlgorithmException | IllegalStateException | UnsupportedEncodingException e) {
e.printStackTrace();
}
return hash;
}
Then, assuming you're interested in having access to the container called mycontainer, this is how you should do:
SharedAccessSignature s = new SharedAccessSignature.SasBuilder()
.signedPermission("rwd")
.signedStart("2018-01-31T10:48:41Z")
.signedExpiry("2018-04-06T18:48:41Z")
.signedVersion("2015-04-05")
.signedResource("c") // <<---- note here
.canonicalizedResource("/blob/globalweb/mycontainer") // No ending slash!
.signedProtocol("https")
.build();
However, if you want to generate an Account SAS, the following code does the trick:
public static void main(String[] args) throws UnsupportedEncodingException {
String accountName = "globalweb";
String signedPermissions = "rl"; //read and list
String signedService = "b"; //blob
String signedResType = "sco"; //service, container, objects
String start = "2018-02-22T17:16:25Z";
String expiry = "2018-02-28T01:16:25Z";
String signedIp = "";
String protocol = "https";
String signedVersion = "2017-07-29";
String stringToSign =
accountName + "\n" +
signedPermissions + "\n" +
signedService + "\n" +
signedResType + "\n" +
start + "\n" +
expiry + "\n" +
signedIp + "\n" +
protocol + "\n" +
signedVersion + "\n";
//outputs SAS Token
System.out.println(
"?sv="+signedVersion +
"&ss="+signedService +
"&srt="+signedResType +
"&sp="+signedPermissions +
"&st="+start+
"&se="+expiry+
"&spr="+protocol+
"&sig="+
URLEncoder.encode(SasBuilder.generateSasSignature(MY_KEY_BASE64, stringToSign), "UTF-8"));
}

Got this same error which led me to this post:
403 Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
In my case I had an EncodeURI() on the already encoded uri.
Removing this also fixed the error.

Please try this if you are using 12.5. I was able to get this working with the following:
try {
//Authenticate
StorageSharedKeyCredential credential = new StorageSharedKeyCredential(this.getAzureAccountName(), this.getAzureAccountKey());
//Get the Blob Service Client
BlobServiceClient client = new BlobServiceClientBuilder()
.endpoint(this.getAzureEndPoint())
.credential(credential)
.buildClient();
//Get the blobContainerClient
BlobContainerClient blobContainerClient =
blobServiceClient.get().getBlobContainerClient(containerName);
BlockBlobClient blockBlobClient = blobContainerClient
.getBlobClient(bolbName)
.getBlockBlobClient();
//Setting Account Permission
AccountSasPermission permissions = new AccountSasPermission()
.setListPermission(true)
.setReadPermission(true);
//Following line is required if signature to be generated at container level
//AccountSasResourceType resourceTypes = new AccountSasResourceType().setContainer(true);
//In case you want to generate the signature at the object level use
AccountSasResourceType resourceTypes = new AccountSasResourceType().setObject(true);
AccountSasService services = new AccountSasService().setBlobAccess(true).setFileAccess(true);
//Valid for 2 days starting today
OffsetDateTime expiryTime = OffsetDateTime.now().plus(Duration.ofDays(2));
AccountSasSignatureValues sasValues =
new AccountSasSignatureValues(expiryTime, permissions, services, resourceTypes);
String sasToken = blobServiceClient.get().generateAccountSas(sasValues);
sasToken = blockBlobClient.getBlobUrl()+"?"+sasToken;
System.out.println("URL to get view the Blob in Browser "+sasToken);
} catch (Exception e) {
log.error("Error = {}",e.getMessage());
}

Related

java.lang.NoClassDefFoundError for com/migcomponents/migbase64/Base64

I am using DocuSign java client first time. I am create custom template for my pdf file. I am using DocuSign APIs. But facing following error:
javax.servlet.ServletException: java.lang.NoClassDefFoundError:
com/migcomponents/migbase64/Base64
com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:420)
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:699)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
I have created REST API and in that API i am trying to use createTemplate code but facing above issue.
I have imported all required jars in my project using maven. All jars are also present in my classpath. It seems at compile time all jar are found but at runtime not able to find the jars.
#Path("/template")
public class CreateTemplate {
private static final String UserName = "xyz.abc#gmail.com";
private static final String UserId = "fcc5726c-cd73-4844-b580-40bbbe6ca126";
private static final String IntegratorKey = "ae30ea4e-3959-4d1c-b867-fcb57d2dc4df";
private static final String IntegratorKeyImplicit = "68c1711f-8b19-47b1-888f-b49b4211d831";
//private static final String ClientSecret = "b4dccdbe-232f-46cc-96c5-b2f0f7448f8f";
private static final String RedirectURI = "https://www.docusign.com/api";
private static final String BaseUrl = "https://demo.docusign.net/restapi";
//private static final String OAuthBaseUrl = "account-d.docusign.com";
private static final String privateKeyFullPath = System.getProperty("user.dir") + "/src/test/keys/docusign_private_key.txt";
private static final String SignTest1File = "C:\\Users\\xyz\\Documents\\testDocuments\\SignTest1.pdf";
//private static final String TemplateId = "cf2a46c2-8d6e-4258-9d62-752547b1a419";
private String[] envelopeIds = new String[0];
TemplateSummary templateSummary;
#GET
#Path("/createTemplate")
#Produces("text/plain")
public TemplateSummary createTemplate() {
System.out.println("\nCreateTemplateTest:\n" + "===========================================");
byte[] fileBytes = null;
File f;
try {
// String currentDir = new java.io.File(".").getCononicalPath();
String currentDir = System.getProperty("user.dir");
java.nio.file.Path path = Paths.get(SignTest1File);
fileBytes = Files.readAllBytes(path);
f = new File(path.toString());
//Assert.assertTrue(f.length() > 0);
System.out.println("f.length()-->"+f.length());
} catch (IOException ioExcp) {
//Assert.assertEquals(null, ioExcp);
ioExcp.printStackTrace();
}
// create an envelope to be signed
EnvelopeTemplate templateDef = new EnvelopeTemplate();
templateDef.setEmailSubject("Please Sign my Java SDK Envelope");
templateDef.setEmailBlurb("Hello, Please sign my Java SDK Envelope.");
// add a document to the envelope
Document doc = new Document();
String base64Doc = Base64.encodeToString(fileBytes, false);
//String base64Doc = Base64.encodeToString(str.getBytes("UTF-8"), false))
doc.setDocumentBase64(base64Doc);
doc.setName("TestFile.pdf");
doc.setDocumentId("1");
List<Document> docs = new ArrayList<Document>();
docs.add(doc);
templateDef.setDocuments(docs);
// Add a recipient to sign the document
Signer signer = new Signer();
signer.setRoleName("Signer1");
signer.setRecipientId("1");
// Create a SignHere tab somewhere on the document for the signer to
// sign
SignHere signHere = new SignHere();
signHere.setDocumentId("1");
signHere.setPageNumber("1");
signHere.setRecipientId("1");
signHere.setXPosition("100");
signHere.setYPosition("100");
signHere.setScaleValue("0.5");
List<SignHere> signHereTabs = new ArrayList<SignHere>();
signHereTabs.add(signHere);
Tabs tabs = new Tabs();
tabs.setSignHereTabs(signHereTabs);
signer.setTabs(tabs);
templateDef.setRecipients(new Recipients());
templateDef.getRecipients().setSigners(new ArrayList<Signer>());
templateDef.getRecipients().getSigners().add(signer);
EnvelopeTemplateDefinition envTemplateDef = new EnvelopeTemplateDefinition();
envTemplateDef.setName("myTemplate");
templateDef.setEnvelopeTemplateDefinition(envTemplateDef);
ApiClient apiClient = new ApiClient(BaseUrl);
//String currentDir = System.getProperty("user.dir");
try {
// IMPORTANT NOTE:
// the first time you ask for a JWT access token, you should grant access by making the following call
// get DocuSign OAuth authorization url:
//String oauthLoginUrl = apiClient.getJWTUri(IntegratorKey, RedirectURI, OAuthBaseUrl);
// open DocuSign OAuth authorization url in the browser, login and grant access
//Desktop.getDesktop().browse(URI.create(oauthLoginUrl));
// END OF NOTE
byte[] privateKeyBytes = null;
try {
privateKeyBytes = Files.readAllBytes(Paths.get(privateKeyFullPath));
} catch (IOException ioExcp) {
//Assert.assertEquals(null, ioExcp);
ioExcp.printStackTrace();
}
if (privateKeyBytes == null) return null;
java.util.List<String> scopes = new ArrayList<String>();
scopes.add(OAuth.Scope_SIGNATURE);
OAuth.OAuthToken oAuthToken = apiClient.requestJWTUserToken(IntegratorKey, UserId, scopes, privateKeyBytes, 3600);
//Assert.assertNotSame(null, oAuthToken);
// now that the API client has an OAuth token, let's use it in all
// DocuSign APIs
apiClient.setAccessToken(oAuthToken.getAccessToken(), oAuthToken.getExpiresIn());
UserInfo userInfo = apiClient.getUserInfo(oAuthToken.getAccessToken());
/*Assert.assertNotSame(null, userInfo);
Assert.assertNotNull(userInfo.getAccounts());
Assert.assertTrue(userInfo.getAccounts().size() > 0);
*/
System.out.println("userInfo.getAccounts().size()-->"+userInfo.getAccounts().size());
System.out.println("UserInfo: " + userInfo);
// parse first account's baseUrl
// below code required for production, no effect in demo (same
// domain)
apiClient.setBasePath(userInfo.getAccounts().get(0).getBaseUri() + "/restapi");
Configuration.setDefaultApiClient(apiClient);
String accountId = userInfo.getAccounts().get(0).getAccountId();
TemplatesApi templatesApi = new TemplatesApi();
templateSummary = templatesApi.createTemplate(accountId, templateDef);
//Assert.assertNotNull(templateSummary);
//Assert.assertNotNull(templateSummary.getTemplateId());
System.out.println("templateSummary-->"+templateSummary);
System.out.println("templateSummary.getTemplateId()-->"+templateSummary.getTemplateId());
//System.out.println("TemplateSummary: " + templateSummary);
} catch (ApiException ex) {
System.out.println("Exception: 123");
ex.printStackTrace();
} catch (Exception e) {
//System.out.println("Exception: " + e.getLocalizedMessage());
System.out.println("Exception: 234");
e.printStackTrace();
}
return templateSummary;
}
}
I feel like I am facing missing runtime jar files.

Apache Camel pool on email doesn't return attachments

This is my first post and english is not my native language, so please don't be cruel.
As i explained in the title I have a program that poll my mail with Apache Camel, and if there's a csv file in the attachments the program take it and process it. When i take the attachments from the email the method exchange.getIn().getAttachments() return an empty container, but I am pretty sure that the email is beeing readed. I am almost sure that I am not doing something regargins SSL but I don't really know what to do.
I am using Java OSGI with annotations and this bundles included:
org.apache.camel.camel-core 2.15.6
org.apache.camel.camel-mail 2.15.6
com.sun.mail.imap 1.6.0
com.sun.mail.javax.mail 1.6.0
javax.mail.api 1.6.0
The source code is on GitHub. Interesting classes are listed below:
ConfigReaderFactory take the email informations via ConfigAdmin and build the url.
#org.osgi.service.component.annotations.Component(name = "factory", property = "service.pid=it.sensorsystem.core.config.factory", configurationPolicy = ConfigurationPolicy.IGNORE)
public class ConfigReaderFactory implements ManagedServiceFactory {
private static final String DELETE_CONDITIONS = "readLock=changed&idempotent=false&noop=true&delete=true";
private static final String NON_DELETE_CONDITIONS = "noop=true";
private volatile DependencyManager dependencyManager;
private final Map<String, Component> components = new HashMap<>();
private String url;
private String name;
private int confLine;
private String endpointType;
private String ip;
private String username;
private String password;
private String folder;
private boolean delete;
private CamelService camel;
private TypeConverter converter;
private EventPublisher publisher;
private Double latitude;
private Double longitude;
private String email;
#Reference(service = TypeConverter.class)
public void setTypeConverter(TypeConverter converter) {
this.converter = converter;
}
public void unsetTypeConverter(TypeConverter converter) {
this.converter = null;
}
#Reference(service = EventPublisher.class)
public void setEventPublisher(EventPublisher publisher) {
this.publisher = publisher;
}
public void unsetEventPublisher(EventPublisher publisher) {
this.publisher = null;
}
#Reference(service = CamelService.class)
public void setCamelService(CamelService camel) {
this.camel = camel;
}
public void unsetCamelService(CamelService camel) {
this.camel = null;
}
#Override
public String getName() {
return this.getClass().getName();
}
#Override
public void updated(String pid, #SuppressWarnings("rawtypes") Dictionary props) throws ConfigurationException {
if (components.containsKey(pid)) {
return;
}
if (props != null) {
List<String> attributes = new ArrayList<>();
List<String> csvTypes = new ArrayList<>();
int count = 1;
String configurationLine;
while ((configurationLine = (String) props.get(Integer.toString(count++))) != null) {
List<String> values = Utils.getValuesFromLine(configurationLine);
attributes.add(values.size() >= 1 ? values.get(0) : TypeConverter.NAMELESS);
csvTypes.add(values.size() >= 2 ? values.get(1) : TypeConverter.NAMELESS);
}
confLine = Integer.parseInt((String) props.get(Config.CONFIG_LINE));
name = (String) props.get(Config.NAME);
initConfigParameters(props);
buildURL();
System.out.println("[URL] " + url);
try {
Map<String, Object> params = new HashMap<>();
params.put(Constants.ATTRIBUTES, attributes);
params.put(Constants.TYPES, csvTypes);
params.put(Constants.CONF_LINE, confLine);
params.put(Constants.SOURCE, name);
params.put(Constants.PUBLISHER, publisher);
params.put(Constants.CONVERTER, converter);
params.put(Constants.LATITUDE, latitude);
params.put(Constants.LONGITUDE, longitude);
params.put(Constants.ENDPOINT_TYPE, endpointType);
params.put(Constants.EMAIL, email);
Processor processor = new CSVProcessor(params);
camel.start(FrameworkUtil.getBundle(this.getClass()).getBundleContext(), processor, url);
} catch (Exception e) {
e.printStackTrace();
}
}
}
#Override
public void deleted(String pid) {
Component component = components.remove(pid);
dependencyManager.remove(component);
component.stop();
}
private void buildURL() {
url = "";
switch(endpointType) {
case Constants.FTP:
url += "ftp://" + username + "#" + ip + "/" + folder + "?";
if(!password.equals("")) {
url += "password=" + password + "&";
}
break;
case Constants.FILE:
url += "file://" + folder + "?";
break;
case Constants.EMAIL:
url += "imaps://imap.gmail.com?username="+email+"&password="+password
+"&delete=false&unseen=false&consumer.delay=100";
}
if(endpointType.equals(Constants.FTP) || endpointType.equals(Constants.FILE)) {
if (delete) {
url += DELETE_CONDITIONS;
} else {
url += NON_DELETE_CONDITIONS;
}
}
}
private void initConfigParameters(#SuppressWarnings("rawtypes") Dictionary props) {
confLine = Integer.parseInt((String) props.get(Config.CONFIG_LINE));
name = (String) props.get(Config.NAME);
endpointType = (String) props.get(Config.ENDPOINT_TYPE);
ip = (String) props.get(Config.IP_ADDRESS);
username = (String) props.get(Config.USERNAME);
password = (String) props.get(Config.PASSWORD);
folder = (String) props.get(Config.FOLDER);
email = (String) props.get(Config.EMAIL);
delete = ((String) props.get(Config.DELETE)).equals("true");
if((String) props.get(Config.LATITUDE)!=null) {
latitude = Double.parseDouble((String) props.get(Config.LATITUDE));
}
if((String) props.get(Config.LONGITUDE)!=null) {
longitude = Double.parseDouble((String) props.get(Config.LONGITUDE));
}
printParameters();
}
private void printParameters() {
System.out.println("\nStarting camel with parameters:");
System.out.println("[sensor_name]::" + name);
System.out.println("[latitude]::" + latitude);
System.out.println("[longitude]::" + longitude);
System.out.println("[endpoint_type]::" + endpointType);
if(endpointType.equals("ftp")) {
System.out.println("[ip_address]::" + ip);
System.out.println("[folder]::" + folder);
System.out.println("[user]::" + username);
System.out.println("[password]::" + password);
} else if(endpointType.equals("file")) {
System.out.println("[folder]::" + folder);
} else if(endpointType.equals("email")) {
System.out.println("[email]::" + email);
System.out.println("[password]::" + password);
}
System.out.println("[delete]::" + delete);
}
}
CSVProcessor take the attachments from the email and process the .csv files.
public class CSVProcessor implements Processor{
private List<String> attributes;
private List<String> csvTypes;
private int confLine;
private String source;
private String endpointType;
private TypeConverter converter;
private EventPublisher publisher;
private Long timestamp;
private Double latitude;
private Double longitude;
#SuppressWarnings("unchecked")
public CSVProcessor(Map<String, Object> params) {
this.attributes = (List<String>) params.get(Constants.ATTRIBUTES);
this.csvTypes = (List<String>) params.get(Constants.TYPES);
this.confLine = (int) params.get(Constants.CONF_LINE);
this.source = (String) params.get(Constants.SOURCE);
this.publisher = (EventPublisher) params.get(Constants.PUBLISHER);
this.converter = (TypeConverter) params.get(Constants.CONVERTER);
this.latitude = (Double) params.get(Constants.LATITUDE);
this.longitude = (Double) params.get(Constants.LONGITUDE);
this.endpointType = (String) params.get(Constants.ENDPOINT_TYPE);
}
#Override
public void process(Exchange exchange) throws Exception {
if(endpointType.equals(Constants.EMAIL)) {
Map<String, DataHandler> attachments = exchange.getIn().getAttachments();
System.out.println("Detected email [attachments: "+exchange.getIn().getAttachments().size()+"]");
System.out.println("[BODY]\n"+exchange.getIn().getBody(String.class));
if(exchange.getIn().hasAttachments()) {
System.out.println("Detected email attachments: "+attachments);
for(String name : attachments.keySet()) {
DataHandler dh = attachments.get(name);
String filename = dh.getName();
System.out.println("Detected email attachment with name: "+filename);
if(filename.contains(".csv")) {
processBody(exchange.getContext().getTypeConverter()
.convertTo(String.class, dh.getInputStream()));
}
}
}
} else {
processBody(exchange.getIn().getBody(String.class));
}
}
private void processBody(String body) {
int count = 1;
for(String line : Utils.getLines(body)){
if(count++>confLine){
for(IOTEvent event: processLine(line)){
publisher.publish(event);
}
}
}
}
private boolean processTimeAndPosition(String line) {
Iterator<String> valueIterator = Utils.getValuesFromLine(line).iterator();
Iterator<String> attributeIterator = attributes.iterator();
Iterator<String> typeIterator = csvTypes.iterator();
boolean systemTimestamp = true;
while(attributeIterator.hasNext()){
String attribute = attributeIterator.next();
String type = typeIterator.next();
String value = valueIterator.next();
switch(attribute.toLowerCase()){
case Constants.TIMESTAMP:
systemTimestamp = false;
timestamp = (Long) converter.convert(type, value);
break;
case Constants.LATITUDE:
latitude = (Double) converter.convert(type, value);
break;
case Constants.LONGITUDE:
longitude = (Double) converter.convert(type, value);
break;
}
}
return systemTimestamp;
}
private List<IOTEvent> processLine(String line){
Iterator<String> valueIterator = Utils.getValuesFromLine(line).iterator();
Iterator<String> attributeIterator = attributes.iterator();
Iterator<String> typeIterator = csvTypes.iterator();
List<IOTEvent> IOTEvents = new ArrayList<>();
boolean systemTimestamp = processTimeAndPosition(line);
// Resetting iterators
valueIterator = Utils.getValuesFromLine(line).iterator();
attributeIterator = attributes.iterator();
typeIterator = csvTypes.iterator();
while(attributeIterator.hasNext()){
String attribute = attributeIterator.next();
String type = typeIterator.next();
String value = valueIterator.next();
switch(attribute.toLowerCase()){
case Constants.TIMESTAMP:
case Constants.LATITUDE:
case Constants.LONGITUDE:
continue;
}
IOTEvent iotEvent = new IOTEvent();
iotEvent.setSource(source);
iotEvent.setValue(new Value(type, converter.convert(type, value)));
if(systemTimestamp) {
iotEvent.setCdate(new Date());
} else {
iotEvent.setCdate(new Date(timestamp));
}
iotEvent.setType(attribute);
iotEvent.setSystemCDate(systemTimestamp);
if(latitude!=null && longitude!=null )
iotEvent.setPoint(new Point(latitude, longitude));
IOTEvents.add(iotEvent);
}
return IOTEvents;
}
}
DefaultCamelService starts camel:
#Component(immediate=true)
public class DefaultCamelService implements CamelService{
private CamelContext camelContext=null;
#Override
public void start(BundleContext context, Processor processor, String url) throws Exception {
if(camelContext==null) {
camelContext = new OsgiDefaultCamelContext(context);
}
camelContext.addRoutes(new RouteBuilder() {
public void configure() {
from(url).process(processor);
}
});
try {
camelContext.start();
} catch(Exception e) {
System.out.println("Error connecting to endpoint:"+url);
}
}
#Override
public void stop() throws Exception {
camelContext.stop();
}
}
This is the result on console that regards email:
Connecting to MailStore: imaps://imap.gmail.com:993 (SSL enabled), folder=INBOX
Getting folder INBOX
Polling mailbox folder: imaps://imap.gmail.com:993 (SSL enabled), folder=INBOX
Configured property: peek on bean: com.sun.mail.imap.IMAPMessage#6dd74603 with value: true
Fetching 1 messages. Total 1 messages.
Processing message: messageNumber=[1], from=[Simone Colaci <sim.colaci#gmail.com>], to=[sensorsystem.csv#gmail.com], subject=[csv testing], sentDate=[Sep 14, 2017 1:15:24 PM], receivedDate=[Sep 14, 2017 1:15:45 PM]
Detected email [attachments: 0]
Close mailbox folder INBOX from imaps://imap.gmail.com:993 (SSL enabled), folder=INBOX
Please help me, remember that this is my first post and english is not my native language. I checked everything on internet.
Thank you.
Edit 1:
This is the body of the email sent by my Gmail account to sensorsystem.csv#gmail.com:
[BODY]
--94eb2c19b976cd06bd055924656c
Content-Type: multipart/alternative; boundary="94eb2c19b976cd06ba055924656a"
--94eb2c19b976cd06ba055924656a
Content-Type: text/plain; charset="UTF-8"
Hi,
this is a csv file
--94eb2c19b976cd06ba055924656a
Content-Type: text/html; charset="UTF-8"
<div dir="ltr"><div>Hi,<br></div>this is a csv file<br><div><div><div class="gmail_quote"><br><div dir="ltr"><br></div>
</div><br></div></div></div>
--94eb2c19b976cd06ba055924656a--
--94eb2c19b976cd06bd055924656c
Content-Type: text/csv; charset="US-ASCII"; name="esrlNcepRe_LonPM180_e063_990c_26e6.csv"
Content-Disposition: attachment;
filename="esrlNcepRe_LonPM180_e063_990c_26e6.csv"
Content-Transfer-Encoding: base64
X-Attachment-Id: f_j7kc6dcl0
dGltZSxsYXRpdHVkZSxsb25naXR1ZGUsYWlyLHByZXMKVVRDLGRlZ3JlZXNfbm9ydGgsZGVncmVl
c19lYXN0LGRlZ0ssUGFzY2FscwoyMDAyLTA3LTAxVDAwOjAwOjAwWiw0Mi41LC0xNDcuNSwyODcu
MSwxMDIyNzAuMAo=
--94eb2c19b976cd06bd055924656c--
Edit 2:
Tried with latest Camel libraries listed below but result is the same:
org.apache.camel.camel-core 2.19.3
org.apache.camel.camel-mail 2.19.3
Edit 3:
Debug also give this warnings from Java Mail:
Sep 18, 2017 10:20:59 AM javax.mail.Session loadResource
WARNING: expected resource not found: /META-INF/javamail.default.providers
Sep 18, 2017 10:20:59 AM javax.mail.Session loadResource
WARNING: expected resource not found: /META-INF/javamail.default.address.map

Aws S3 access denied issue

I am trying to access s3 bucket. I am able to do so using my local machine(i.e. from my local machine to S3 bucket), but getting access denied issue while trying to access it from EC2 instance running tomcat 8 and java 8.
Also when i upload the file the permissions are set for root user and if I keep my bucket as public and upload the file from EC2 the permissions are not set for the root user.
public class AmazonS3UtilService {
public static final String NAME = "amazonS3Util";
private static String S3_SECRET = "S3_SECRET";
private static String S3_ID = "S3_ID";
private static String BUCKET_NAME = "S3_BUCKET";
private static final String SUFFIX = "/";
private static final String DEFAULT_FOLDER_PATH = "PHR/Reports/";
#Autowired
protected Environment props;
private AWSCredentials awsCredentials = null;
private AmazonS3 s3Client = null;
private String bucketName = null;
private static final Logger log = Logger.getLogger(AmazonS3UtilService.class);
private void prepareAWSCredentials() {
if (awsCredentials == null) {
log.info("Preparing AWS Credentials");
awsCredentials = new AWSCredentials() {
#SuppressWarnings("unused")
Map<String, String> env = System.getenv();
public String getAWSSecretKey() {
String S3_SECRET = System.getProperty(AmazonS3UtilService.S3_SECRET);
if (S3_SECRET == null) {
S3_SECRET = System.getenv(AmazonS3UtilService.S3_SECRET);
if (S3_SECRET == null) {
S3_SECRET = props.getProperty(AmazonS3UtilService.S3_SECRET);
}
}
log.info("S3_SECRET ---->" + S3_SECRET);
return S3_SECRET;
}
public String getAWSAccessKeyId() {
String S3_ID = System.getProperty(AmazonS3UtilService.S3_ID);
if (S3_ID == null) {
S3_ID = System.getenv(AmazonS3UtilService.S3_ID);
if (S3_ID == null) {
S3_ID = props.getProperty(AmazonS3UtilService.S3_ID);
}
}
log.info("S3_ID ---->" + S3_ID);
return S3_ID;
}
};
}
}
private void prepareAmazonS3Client() {
if (s3Client == null) {
log.info("Preparing S3 Client");
ClientConfiguration clientCfg = new ClientConfiguration();
clientCfg.setProtocol(Protocol.HTTP);
s3Client = new AmazonS3Client(awsCredentials, clientCfg);
Region region = Region.getRegion(Regions.fromName(props.getProperty("s3client.region")));
log.info("Region ----->" + props.getProperty("s3client.region"));
s3Client.setRegion(region);
}
}
private void prepareBucketName() {
bucketName = System.getenv(AmazonS3UtilService.BUCKET_NAME);
log.info("bucketName ------>" + bucketName);
}
}
private void prepare() {
try {
awsCredentials = null;
prepareAWSCredentials();
prepareAmazonS3Client();
prepareBucketName();
} catch (AmazonServiceException ase) {
log.error("Caught an AmazonServiceException, which means your request made it "
+ "to Amazon S3, but was rejected with an error response for some reason.");
log.error("Error Message: " + ase.getMessage() + " HTTP Status Code: " + ase.getStatusCode()
+ " AWS Error Code: " + ase.getErrorCode() + " Error Type: " + ase.getErrorType()
+ " Request ID: " + ase.getRequestId());
new AmazonS3ClientException(ase, ase.getMessage());
} catch (AmazonClientException ace) {
log.error(ace);
new AmazonS3ClientException(ace, ace.getMessage());
}
}
#SuppressWarnings("unused")
public String uploadDocument(UploadDocumentDetailDTO uploadDocumentDetail) {
prepare();
String tempFileName = new SimpleDateFormat("yyyy-MM-dd hh-mm-ss").format(new Date());
String fileURL = null;
try {
File uploadFileContent = readBase64File(uploadDocumentDetail.getFileContent(), tempFileName);
uploadDocumentDetail.setContentType(FileContentTypeEnum.PDF);
String uploadFileName = getUploadFileName(uploadDocumentDetail);
PutObjectRequest request = new PutObjectRequest(bucketName, uploadFileName, uploadFileContent);
request.putCustomRequestHeader("Content-Type", "application/pdf");
request.putCustomRequestHeader("Content-Disposition", "inline");
PutObjectResult putObjectResult = s3Client.putObject(request);
URL url = generatePresignedUrlRequest(uploadFileName);
fileURL = url.toString();
} catch (Exception e) {
log.info(LoggerException.printException(e));
fileURL = "";
}
return fileURL;
}
public URL generatePresignedUrlRequest(String fileURL) {
log.info("Inside generatePresignedUrlRequest");
java.util.Date expiration = new java.util.Date();
long msec = expiration.getTime();
msec += 1000 * 60 * 60; // 1 hour.
expiration.setTime(msec);
GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, fileURL);
generatePresignedUrlRequest.setMethod(HttpMethod.GET); // Default.
generatePresignedUrlRequest.setExpiration(expiration);
URL s = s3Client.generatePresignedUrl(generatePresignedUrlRequest);
log.info("Url --->" + s);
return s;
}
private String getUploadFileName(UploadDocumentDetailDTO uploadDocumentDetail) {
StringBuffer uploadFileName = new StringBuffer();
uploadFileName.append(DEFAULT_FOLDER_PATH);
if (uploadDocumentDetail.getBeneficiaryId() != null)
uploadFileName.append(uploadDocumentDetail.getBeneficiaryId() + SUFFIX);
if (uploadDocumentDetail.getDocumentType() != null)
uploadFileName.append(uploadDocumentDetail.getDocumentType().getName() + SUFFIX);
// Check and create Folder
validateAndCreateFolder(uploadFileName.toString());
if (uploadDocumentDetail.getAssesmentId() != null)
uploadFileName.append(
uploadDocumentDetail.getAssesmentId() + "." + uploadDocumentDetail.getContentType().getName());
else
uploadFileName.append(
uploadDocumentDetail.getDefaultFileName() + "." + uploadDocumentDetail.getContentType().getName());
return uploadFileName.toString();
}
private static File readBase64File(String content, String fileName) throws Exception {
File file = File.createTempFile(fileName, ".tmp");
file.deleteOnExit();
FileOutputStream fileOuputStream = new FileOutputStream(file);
fileOuputStream.write(Base64.decodeBase64(content));
fileOuputStream.close();
return file;
}
public void validateAndCreateFolder(String folderName) {
List<S3ObjectSummary> fileList = null;
try {
fileList = s3Client.listObjects(bucketName, folderName).getObjectSummaries();
} catch (AmazonServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (AmazonClientException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (fileList == null || fileList.isEmpty()) {
// create meta-data for your folder and set content-length to 0
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(0);
// create empty content
InputStream emptyContent = new ByteArrayInputStream(new byte[0]);
// create a PutObjectRequest passing the folder name suffixed by /
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, folderName, emptyContent, metadata);
// send request to S3 to create folder
s3Client.putObject(putObjectRequest);
}
}
/**
* This method first deletes all the files in given folder and than the
* folder itself
*/
}
Following is the exception while access S3 from EC2 instance.
INFO com.medscheme.common.util.AmazonS3UtilService - com.amazonaws.services.s3.model.AmazonS3Exception: Access Denied (Service: Amazon S3; Status Code: 403; Error Code: AccessDenied; Request ID: 926E1213366626B9), S3 Extended Request ID: zQbb4JCalYExHZtDSv0GmWxoHrQZJUV3M+jlUiaVJY/sDxW/qoNFC8hizfangVCjweWZtOqC7/A=
at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:1275)
at com.amazonaws.http.AmazonHttpClient.executeOneRequest(AmazonHttpClient.java:873)
at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:576)
at com.amazonaws.http.AmazonHttpClient.doExecute(AmazonHttpClient.java:362)
at com.amazonaws.http.AmazonHttpClient.executeWithTimer(AmazonHttpClient.java:328)
at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:307)
at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:3649)
at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:3602)
at com.amazonaws.services.s3.AmazonS3Client.listObjects(AmazonS3Client.java:679)
at com.amazonaws.services.s3.AmazonS3Client.listObjects(AmazonS3Client.java:664)
at com.medscheme.common.util.AmazonS3UtilService.validateAndCreateFolder(AmazonS3UtilService.java:222)
at com.medscheme.common.util.AmazonS3UtilService.getUploadFileName(AmazonS3UtilService.java:200)
at com.medscheme.common.util.AmazonS3UtilService.uploadDocument(AmazonS3UtilService.java:166)
at com.medscheme.service.impl.ReportsServiceImpl.getReport(ReportsServiceImpl.java:133)
at com.medscheme.service.impl.ReportsServiceImpl.getReport(ReportsServiceImpl.java:1)
at com.medscheme.controller.ReportsController.getWellnessReportDetails(ReportsController.java:69)
I was able to resolve the issue by using BasicAWSCredentials class instead of AWSCredentials while creating the amazon client.
The problem was only with EC2 instance.
Does anybody know what was going wrong on EC2.

Google Cloud Storage Signed URL Where to put private key file [duplicate]

I am using jersey-client to make REST Call . I am getting SignatureDoesNotMatch error in response.
I was trying to List down Bucket names using GET Service , also tried to list Bucket object using GET Bucket method.
here is my sample code.
Any hint or solution ?
public class restSample {
private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1";
private static final String PROJECT_ID = "10XXXXXXXX478";
public static String Base64Encoding()
throws java.security.SignatureException, UnsupportedEncodingException {
String access_id = "GOOGBAXXXXXXXXXXBI";
String secret_key = URLEncoder.encode("pWTXXXXXXXXXXXXXXXRo85T+XXXXXXXXX3O","UTF-8");
String bucket = "bucket_name";
String version_header = "x-goog-api-version:1";
String project_header = "x-goog-project-id:"+PROJECT_ID;
String canonicalizedResources = "/"+bucket+"/";
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MINUTE, 30);
long expiration = calendar.getTimeInMillis();
String stringToSign = URLEncoder.encode("GET\n\n\n"+expiration+"\n"+version_header+"\n"+project_header+"\n"+canonicalizedResources,"UTF-8");
//String stringToSign = URLEncoder.encode("GET\n\n\n"+getdate()+"\n"+version_header+"\n"+project_header+"\n"+canonicalizedResources,"UTF-8");
String authSignature="";
try {
SecretKeySpec signingKey = new SecretKeySpec(secret_key.getBytes(),HMAC_SHA1_ALGORITHM);
Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
mac.init(signingKey);
// compute the hmac on input data bytes
byte[] rawHmac = mac.doFinal(stringToSign.getBytes("UTF-8"));
// base64-encode the hmac
authSignature = new String(Base64.encode(rawHmac));
} catch (Exception e) {
throw new SignatureException("Failed to generate HMAC : " + e.getMessage());
}
authSignature = (access_id +":"+ authSignature);
return authSignature;
}
public static void main(String[] args) {
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
String authSignature = null;
try {
authSignature = "GOOG1 "+ Base64Encoding();
} catch (SignatureException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
WebResource service = client.resource(getBaseURI());
ClientResponse response = service.accept(MediaType.APPLICATION_XML)
.header("Authorization",authSignature)
.header("Date", getdate())
.header("Content-Length", "0")
.header("x-goog-api-version", "1")
.header("x-goog-project-id", PROJECT_ID)
.get(ClientResponse.class);
System.out.println(response.getClientResponseStatus().getFamily());
System.out.println("response1 :: " + response.getEntity(String.class));
}
private static URI getBaseURI() {
String url = "https://bucket_name.storage.googleapis.com";
return UriBuilder.fromUri(url).build();
}
private static String getdate(){
SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z ", new Locale("US"));
Calendar cal = Calendar.getInstance(new SimpleTimeZone(0, "GMT"));
format.setCalendar(cal);
return format.format(new Date());
}
}
Thanks!
Make sure the string you are signing matches the expected string to sign. Google cloud storage returns the expected string to sign in the HTTP response if authentication fails.
In your particular example it looks like you are adding both the version_header and project_header into the string to sign. These are not in the list of CanonicalHeaders nor CanonicalExtensionHeaders, so you are signing a different string than the server.
You can review the list here: https://developers.google.com/storage/docs/reference/v1/developer-guidev1#authentication

help needed in importing yahoo contacts to my page through play framework

I am working in Play framework and I am trying to import yahoo contacts.
I have cleared the yahoo authentication api's to get the access_token and guid.
With that, when I try to import the contacts using http://social.yahooapis.com/v1/user/{guid}/contacts with the auth parameters, I am getting the connection timeout exception in my page and log.
When I paste the same contact url being generated through the code in the browser, I am getting as signature_invalid
I hope I have followed all the stuffs mentioned in the yahoo api dev notes to create the oauth_signature, but still I am not getting it.
Can anyone help me on this please?
Controller code for generating signature -
public class Yahoo {
private static String token = "";
private static String currentUrl = "";
private static String verifier = "";
private static String tokenSecret = "";
private static String accessToken = "";
private static String yahooGuid = "";
public Yahoo(){
}
/**
* Requests access to the Yahoo API for request token.
* #return True if the request is successful, false if not.
*/
public static Yahoo authorize() {
Session session = Session.current();
if(session.contains("authorized") && session.get("authorized").equals("0")){
session.put("authorized", "1");
String url = getRequestTokenUrl();
WS.WSRequest request = WS.url(url);
Logger.info("Yahoo: Create request to get request token'%s'", request.url);
WS.HttpResponse response = request.get();
Logger.info("Yahoo: Token response status is %d", response.getStatus());
if (response.getStatus() == 200){
String[] pairs = response.getString().split("&");
String[] tokenSecret = pairs[1].split("=");
Yahoo.tokenSecret = tokenSecret[1];
for (String pair : pairs) {
String[] kv = pair.split("=");
if (kv.length != 2) {
break;
} else {
if (kv[0].equals("oauth_token")) {
Yahoo.token = kv[1];
}
}
}
Logger.info("level 1 - yahoo token = %s, secret = %s",Yahoo.token, Yahoo.tokenSecret);
}
}
return null;
}
/**
* Requests access to the Yahoo API for access token.
* #return True if the request is successful, false if not.
*/
public static Yahoo getAccessToken(){
String url = getAccessTokenUrl();
WS.WSRequest request = WS.url(url);
Logger.info("Yahoo: Create request to get Access token'%s'", request.url);
WS.HttpResponse response = request.get();
Logger.info("Yahoo: Token response status is %d", response.getStatus());
if (response.getStatus() == 200){
String[] pairs = response.getString().split("&");
String[] guidPair = pairs[5].split("=");
String[] tokenSecret = pairs[1].split("=");
Yahoo.tokenSecret = tokenSecret[1];
yahooGuid = guidPair[1];
for (String pair : pairs) {
String[] kv = pair.split("=");
if (kv.length != 2) {
break;
} else {
if (kv[0].equals("oauth_token")) {
Yahoo.accessToken = kv[1];
}
}
}
Logger.info("level 3 - yahoo token = %s, secret = %s, guid = %s",Yahoo.accessToken, Yahoo.tokenSecret, Yahoo.yahooGuid);
}
return null;
}
/**
* Requests Signature
* #return String
*/
public static String getBaseSignature(){
String signature = "";
String data = generateBaseString();
String key = keyString();
Logger.info("key : %s",key);
try {
SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), "HmacSHA1");
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(signingKey);
byte[] rawHmac = mac.doFinal(data.getBytes());
signature = new String(Base64.encode(rawHmac));
signature = java.net.URLEncoder.encode(signature, "ISO-8859-1");
Logger.info("Signature=%s", signature);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return signature;
}
/**
* Requests access to the Yahoo API for getting contacts.
*
*/
public static void getContacts(){
String url = getContactUrl();
WS.WSRequest request = WS.url(url);
Logger.info("Yahoo: Create request to get Contacts '%s'", request.url);
WS.HttpResponse response = request.get();
Logger.info("Yahoo: Token response status is %d", response.getStatus());
if (response.getStatus() == 200){
String[] pairs = response.getString().split("&");
for(int i=0;i<pairs.length;i++){
Logger.info("%s", pairs[i]);
}
}else {
//errors contains a JSON response
JsonParser parser = new JsonParser();
JsonObject message = parser.parse(response.getString()).getAsJsonObject();
Logger.error("Yahoo: Could not get token (status %d): %s", response.getStatus(), message.get("message").getAsString());
}
}
public static String generateBaseString(){
String baseString = getBaseUrl();
Logger.info("token secret : %s",tokenSecret);
Logger.info("base url : %s",baseString);
Logger.info("callback url : %s",getCallBackUrl().toString().split("oauth_token")[0].replace('?', '\0'));
String returnString = "";
try {
returnString = java.net.URLEncoder.encode("GET", "ISO-8859-1") + "&" + java.net.URLEncoder.encode("http://social.yahooapis.com/v1/user/"+yahooGuid+"/contacts", "ISO-8859-1") + "&" + java.net.URLEncoder.encode(baseString, "ISO-8859-1");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Logger.info("Yahoo: Base string: %s",returnString);
return returnString;
}
public static String keyString(){
String consumerSecret = encodeString(getConsumerSecret());
String tokenSec = encodeString(tokenSecret);
String keyString = consumerSecret + encodeString("&") + tokenSec;
return keyString;
}
public static String encodeString(String msgString){
String msg = "";
try {
msg = java.net.URLEncoder.encode(msgString.toString(), "ISO-8859-1");
Logger.info("encode=%s", msg);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return msg;
}
You are getting invalid signature when you copy and paste on the browser because you are not passing the oauth headers.
I strongly suggest you using a lib to do that, I have done the exact same thing with linkedin:
http://geeks.aretotally.in/projects/play-framework-linkedin-module
You will find explanation and link to the source code.
Hope it helps.
Thank you,
Felipe
http://geeks.aretotally.in
http://playframework.info
http://mashup.fm

Categories

Resources