Java读取Evernote中如何使用DeveloperToken
发表于:2025-02-05 作者:千家信息网编辑
千家信息网最后更新 2025年02月05日,这篇文章主要为大家展示了"Java读取Evernote中如何使用DeveloperToken",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"Java读取Ev
千家信息网最后更新 2025年02月05日Java读取Evernote中如何使用DeveloperToken
这篇文章主要为大家展示了"Java读取Evernote中如何使用DeveloperToken",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"Java读取Evernote中如何使用DeveloperToken"这篇文章吧。
首先我们需要在Evernote的sandbox环境注册一个账户,https://sandbox.evernote.com/Registration.action
有账户,如果希望使用java读取账户的信息,我们还需要申请一个Token,也就是一个授权码,有了授权,我们才能从账户里那东西。https://sandbox.evernote.com/api/DeveloperToken.action
开发准备就绪,下面创建一个Maven项目,引入Evernote API
创建Maven项目:
mvn archetype:generate -DgroupId=com.yotoo.app -DartifactId=simple -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
添加evernote jdk依赖:
4.0.0 com.yotoo.app simple jar 1.0-SNAPSHOT simple http://maven.apache.org com.evernote evernote-api 1.25.1 junit junit 3.8.1 test
4. 把项目导入Intellij IDEA或Eclipse随你喜欢,创建一个HelloEvernote类
HelloEvernote类
package com.yotoo.app;import com.evernote.auth.EvernoteAuth;import com.evernote.auth.EvernoteService;import com.evernote.clients.ClientFactory;import com.evernote.clients.NoteStoreClient;import com.evernote.clients.UserStoreClient;import com.evernote.edam.error.EDAMErrorCode;import com.evernote.edam.error.EDAMSystemException;import com.evernote.edam.error.EDAMUserException;import com.evernote.edam.notestore.NoteFilter;import com.evernote.edam.notestore.NoteList;import com.evernote.edam.type.*;import com.evernote.thrift.transport.TTransportException;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.security.MessageDigest;import java.util.Iterator;import java.util.List;public class HelloEvernote { // 去这里申请一个 Token https://sandbox.evernote.com/api/DeveloperToken.action private static final String AUTH_TOKEN = "填写授权Token"; private UserStoreClient userStore; private NoteStoreClient noteStore; private String newNoteGuid; public static void main(String args[]) throws Exception { String token = System.getenv("AUTH_TOKEN"); if (token == null) { token = AUTH_TOKEN; } if ("".equals(token)) { System.err.println("去这里申请一个Token https://sandbox.evernote.com/api/DeveloperToken.action"); return; } HelloEvernote demo = new HelloEvernote(token); try { demo.listNotes(); demo.createNote(); demo.searchNotes(); demo.updateNoteTag(); } catch (EDAMUserException e) { // 需要处理的异常 if (e.getErrorCode() == EDAMErrorCode.AUTH_EXPIRED) { System.err.println("授权的Token已经过期!"); } else if (e.getErrorCode() == EDAMErrorCode.INVALID_AUTH) { System.err.println("无效的授权Token!"); } else if (e.getErrorCode() == EDAMErrorCode.QUOTA_REACHED) { System.err.println("无效的授权Token!"); } else { System.err.println("错误: " + e.getErrorCode().toString() + " 参数: " + e.getParameter()); } } catch (EDAMSystemException e) { System.err.println("系统错误: " + e.getErrorCode().toString()); } catch (TTransportException t) { System.err.println("网络错误:" + t.getMessage()); } } /** * 初始化 UserStore and NoteStore 客户端 */ public HelloEvernote(String token) throws Exception { // 设置 UserStore 的客户端并且检查和服务器的连接 EvernoteAuth evernoteAuth = new EvernoteAuth(EvernoteService.SANDBOX, token); ClientFactory factory = new ClientFactory(evernoteAuth); userStore = factory.createUserStoreClient(); boolean versionOk = userStore.checkVersion("Evernote EDAMDemo (Java)", com.evernote.edam.userstore.Constants.EDAM_VERSION_MAJOR, com.evernote.edam.userstore.Constants.EDAM_VERSION_MINOR); if (!versionOk) { System.err.println("不兼容的Evernote客户端协议"); System.exit(1); } // 设置 NoteStore 客户端 noteStore = factory.createNoteStoreClient(); } /** * 获取并显示用户的笔记列表 */ private void listNotes() throws Exception { // 列出用户的Notes System.out.println("Listing notes:"); // 首先,获取一个笔记本的列表 List
notebooks = noteStore.listNotebooks(); for (Notebook notebook : notebooks) { System.out.println("Notebook: " + notebook.getName()); // 然后,搜索笔记本中前100个笔记并按创建日期排序 NoteFilter filter = new NoteFilter(); filter.setNotebookGuid(notebook.getGuid()); filter.setOrder(NoteSortOrder.CREATED.getValue()); filter.setAscending(true); NoteList noteList = noteStore.findNotes(filter, 0, 100); List notes = noteList.getNotes(); for (Note note : notes) { System.out.println(" * " + note.getTitle()); } } System.out.println(); } /** * 创建一个新的笔记 */ private void createNote() throws Exception { // 创建一个新的笔记对象,并填写相关内容,比如标题等 Note note = new Note(); note.setTitle("Yotoo的Demo演示:通过Java创建一个新的笔记"); String fileName = "enlogo.png"; String mimeType = "image/png"; // 给笔记添加一个附件,比如图片;首先创建一个资源对象,然后设置相关属性,比如文件名 Resource resource = new Resource(); resource.setData(readFileAsData(fileName)); resource.setMime(mimeType); ResourceAttributes attributes = new ResourceAttributes(); attributes.setFileName(fileName); resource.setAttributes(attributes); //现在,给新的笔记增加一个新的资源对象 note.addToResources(resource); // 这个资源对象作为笔记的一部分显示 String hashHex = bytesToHex(resource.getData().getBodyHash()); // Evernote笔记的内容是通过ENML(Evernote Markup Language)语言生成的。 // 在这里可以了解具体的说明 http://dev.evernote.com/documentation/cloud/chapters/ENML.php String content = "" + "" + " " + "Evernote的图标在这里,很绿奥! "; note.setContent(content); // 最后,使用createNote方法,发送一个新的笔记给Evernote。 // 返回的新笔记对象将包含服务器生成的属性,如新笔记的的GUID Note createdNote = noteStore.createNote(note); newNoteGuid = createdNote.getGuid(); System.out.println("成功创建一个新的笔记,GUID为:" + newNoteGuid); System.out.println(); } /** * 查询用户的笔记并显示结果 */ private void searchNotes() throws Exception { // 搜索的格式需要根据Evernote搜索语法, // 参考这里http://dev.evernote.com/documentation/cloud/chapters/Searching_notes.php // 在这个例子中,我们搜索的标题中包含Yotoo String query = "intitle:Yotoo"; // 搜索的笔记中包含具体标签,我可以使用这个: // String query = "tag:tagname"; // 搜索任何位置包含"Yotoo"的笔记,可以使用: // String query = "Yotoo"; NoteFilter filter = new NoteFilter(); filter.setWords(query); filter.setOrder(NoteSortOrder.UPDATED.getValue()); filter.setAscending(false); // 查询前50个满足条件的笔记 System.out.println("满足查询条件的笔记: " + query); NoteList notes = noteStore.findNotes(filter, 0, 50); System.out.println("找到 " + notes.getTotalNotes() + " 个笔记"); Iterator
" + "" + " iter = notes.getNotesIterator(); while (iter.hasNext()) { Note note = iter.next(); System.out.println("笔记: " + note.getTitle()); // 通过findNotes()返回的Note对象,仅包含笔记的属性,标题,GUID,创建时间等,但笔记的内容和二进制数据都省略了; // 获取笔记的内容和二进制资源,可以调用getNote()方法获取 Note fullNote = noteStore.getNote(note.getGuid(), true, true, false, false); System.out.println("笔记包含 " + fullNote.getResourcesSize() + " 个资源对象"); System.out.println(); } } /** * 更新标签分配给一个笔记。这个方法演示了如何调用updateNote方法发送被修改字段 */ private void updateNoteTag() throws Exception { // 当更新一个笔记时,只需要发送已经修改的字段。 // 例如,通过updateNote发送的Note对象中没有包含资源字段,那么Evernote服务器不会修改已经存在的资源属性 // 在示例代码中,我们获取我们先前创建的笔记,包括 笔记的内容和所有资源。 Note note = noteStore.getNote(newNoteGuid, true, true, false, false); //现在,更新笔记。复原内容和资源,因为没有修改他们。我们要修改的是标签。 note.unsetContent(); note.unsetResources(); // 设置一个标签 note.addToTagNames("TestTag"); // 现在更新笔记,我们没有设置内容和资源,所以他们不会改变 noteStore.updateNote(note); System.out.println("成功增加标签"); // 证明一下我们没有修改笔记的内容和资源;重新取出笔记,它仍然只有一个资源(图片) note = noteStore.getNote(newNoteGuid, false, false, false, false); System.out.println("更新以后, 笔记有 " + note.getResourcesSize() + " 个资源"); System.out.println("更新以后,笔记的标签是: "); for (String tagGuid : note.getTagGuids()) { Tag tag = noteStore.getTag(tagGuid); System.out.println("* " + tag.getName()); } System.out.println(); } /** * 从磁盘读取文件的内容并创建数据对象 */ private static Data readFileAsData(String fileName) throws Exception { String filePath = new File(HelloEvernote.class.getResource( "com.yotoo.app.HelloEvernote.class").getPath()).getParent() + File.separator + fileName; // 读取文件的二进制内容 FileInputStream in = new FileInputStream(filePath); ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); byte[] block = new byte[10240]; int len; while ((len = in.read(block)) >= 0) { byteOut.write(block, 0, len); } in.close(); byte[] body = byteOut.toByteArray(); // 创建一个新的包含文件内容的二进制对象 Data data = new Data(); data.setSize(body.length); data.setBodyHash(MessageDigest.getInstance("MD5").digest(body)); data.setBody(body); return data; } /** * 把byte数组转换成hexadecimal字符串 */ public static String bytesToHex(byte[] bytes) { StringBuilder sb = new StringBuilder(); for (byte hashByte : bytes) { int intVal = 0xff & hashByte; if (intVal < 0x10) { sb.append('0'); } sb.append(Integer.toHexString(intVal)); } return sb.toString(); }}
5. 把Evernote的图标放在HelloEvernote 类的同级目录。
6. 执行一下看看,如果在Evernote账户下创建了一个新的笔记,说明已经学会啦。
以上是"Java读取Evernote中如何使用DeveloperToken"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!
笔记
内容
资源
对象
标签
搜索
更新
账户
二进制
客户
客户端
属性
文件
方法
字段
服务器
标题
用户
篇文章
错误
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
java数据库传到前端
网络技术和人工智能
网络安全审查申报
修改服务器端口 不重启
乡镇财政所网络安全管理制度
数据库管理系统哪三个阶段
如何查找数据库字段
网络安全狗安装
网络安全背景文件
江苏远程指导软件开发专业服务
数据库安全性包括什么区别
ldap导入导出数据库
软件属于电子数据库
php数据库修改密码
查看数据库管理员
网络安全新技术有哪些
电信服务器 费用
昆明软件开发工程师培训学院
网络安全起源是什么
网络安全组织领导规章制度
app软件开发价目表
小米路由器3g做打印服务器
中学生网络安全竞赛网址
中关村互联网科技北京百知
java就是软件开发吗
互联网高科技视频素材
镇海计算机软件开发工程
我的世界服务器怎么设置和删除家
数据库更新数据的语法
app服务器怎么关闭签名