最近在研究flickr以及它的API,为了测试它的API,写了一个脚本,这样就可以很方便地测试它提供的API了。
参考这个地方:http://www.flickr.com/services/api/auth.spec.html

PS.你先要修改脚本中的:
API_KEY=****************************(这里是api_key)
API_SECRET=************************(这里是对应的公钥)
改成你自己的KEY,而且要先编译底下的那个md5.java文件,以确认它能够正常对字符串进行加密。

pjq@gentoo-pjq ~/flickr $ cat flickr.sh
#########################################################################
# Author: [email protected]
# Created Time: Mon 03 Aug 2009 07:48:46 PM CST
# File Name: flickr.sh
# Description:
#########################################################################
#!/bin/bash

API_KEY=****************************
API_SECRET=************************
#echo "API_KEY=${API_KEY}"
#echo "API_SECRET=${API_SECRET}"

echo "*********************************************************"
echo "Get frob..."
echo "*********************************************************"
URL=http://flickr.com/services/rest/?
METHORD=flickr.auth.getFrob
SIG=${API_SECRET}api_key${API_KEY}method${METHORD}
echo SIG=${SIG}
API_SIG=`java md5 ${SIG}`
echo API_SIG=${API_SIG}
FLICKR_URL="${URL}method=${METHORD}&api_key=${API_KEY}&api_sig=${API_SIG}"
echo FLICKR_URL=${FLICKR_URL}
wget  ${FLICKR_URL} -O frob.xml

FROB=`cat frob.xml |grep frob|cut -d "<" -f2|cut -d ">" -f2`
echo FROB=${FROB}

echo "*********************************************************"
echo "Load firefox to Confirm the authentication."
echo "*********************************************************"
SIG=${API_SECRET}api_key${API_KEY}frob${FROB}permswrite
echo SIG=${SIG}
API_SIG=`java md5 ${SIG}`
echo API_SIG=${API_SIG}

FLICKR_URL="http://flickr.com/services/auth/?api_key=${API_KEY}&perms=write&frob=${FROB}&api_sig=${API_SIG}"
echo "Loading ${FLICKR_URL}"
firefox ${FLICKR_URL}
read -p "Check OK[yes/no]:" CHOOSE
echo Your input:${CHOOSE}

echo "*********************************************************"
echo "Get auth token..."
echo "*********************************************************"
METHORD=flickr.auth.getToken
SIG=${API_SECRET}api_key${API_KEY}frob${FROB}method${METHORD}
echo SIG=${SIG}
API_SIG=`java md5 ${SIG}`
echo API_SIG=${API_SIG}

FLICKR_URL="http://flickr.com/services/rest/?method=flickr.auth.getToken&api_key=${API_KEY}&frob=${FROB}&api_sig=${API_SIG}"

wget  ${FLICKR_URL} -O token.xml
#cat token.xml
TOKEN=`cat token.xml|grep -i token|cut -d ">" -f2|cut -d "<" -f1`
echo TOKEN=${TOKEN}
echo "*********************************************************"
echo "Now you can use this token to Communication with flickr.com"
echo "Here I just get the info and the blog list I have setted in the flickr."
echo "*********************************************************"

echo "*********************************************************"
echo "Get Info..."
echo "*********************************************************"
METHORD=flickr.people.getInfo
SIG=${API_SECRET}api_key${API_KEY}auth_token${TOKEN}method${METHORD}
echo SIG=${SIG}
API_SIG=`java md5 ${SIG}`
echo API_SIG=${API_SIG}
FLICKR_URL="http://flickr.com/services/rest/?method=${METHORD}&api_key=${API_KEY}&auth_token=${TOKEN}&api_sig=${API_SIG}"

wget  ${FLICKR_URL} -O info.xml
cat info.xml

echo "*********************************************************"
echo "Get blog list..."
echo "*********************************************************"
METHORD=flickr.blogs.getList
SIG=${API_SECRET}api_key${API_KEY}auth_token${TOKEN}method${METHORD}
echo SIG=${SIG}
API_SIG=`java md5 ${SIG}`
echo API_SIG=${API_SIG}
FLICKR_URL="http://flickr.com/services/rest/?method=${METHORD}&api_key=${API_KEY}&auth_token=${TOKEN}&api_sig=${API_SIG}"

wget  ${FLICKR_URL} -O bloglist.xml
cat bloglist.xml

由于过程之间需要对字符串进行MD5加密,所以我另外加了一个md5加密的小程序,用来对字符串进行md5加密,用的是java程序,参数为你需要加密的字符串:


pjq@gentoo-pjq ~/flickr $ cat md5.java
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class md5
{
	public String	str;

	public void md5s(String plainText)
	{
		try
		{
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(plainText.getBytes());
			byte b[] = md.digest();

			int i;

			StringBuffer buf = new StringBuffer("");
			for (int offset = 0; offset < b.length; offset++)
			{
				i = b[offset];
				if (i < 0)
					i += 256;
				if (i < 16)
					buf.append("0");
				buf.append(Integer.toHexString(i));
			}
			str = buf.toString();
			System.out.println(buf.toString());// 32位的加密
			//System.out.println("result(md5,32): " + buf.toString());// 32位的加密
			//System.out.println("result(md5,16): " + buf.toString().substring(8, 24));// 16位的加密
		} catch (NoSuchAlgorithmException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();

		}
	}

	public static void main(String args[])
	{
		md5 md51 = new md5();
		for (String arg : args)
		{
			//System.out.println("进行md5加密:"+arg);
			md51.md5s(arg);

		}


	}

}

所以要用到上面的那个脚本,必须先对这个JAVA程序进行编译:

javac md5.java

然后调用的时候在后面加上你要进行md5加密的字符串:

pjq@gentoo-pjq ~/flickr $ java md5 yourstring
0653fc45290cd602be895bb8d054942c

在脚本里我是这样处理的:

API_SIG=`java md5 ${SIG}`

直接将它转换后的字符串用来赋值。

运行这个脚本的过程中需要与Flickr进行权限确认,所以中途要等你通过firefox确认之后,输入yes,再往下运行。
最终会在目录下生成文件bloglist.xml,里面记录了你已经设置好的blog列表:

pjq@gentoo-pjq ~/flickr $ cat bloglist.xml
<?xml version="1.0" encoding="utf-8" ?>
<rsp stat="ok">
<blogs>
	<blog id="72157621754711439" name="予人玫瑰,手留余香" service="MetaWeblogAPI" needspassword="0" url="http://www.impjq.net/blog/" />
	<blog id="72157621805025161" name="Twitter: pengjianqing" service="Twitter" needspassword="0" url="http://twitter.com/pengjianqing" />
</blogs>
</rsp>

上面两个链接就是我自己的设置。
至此,简单的验证了Flickr的API。

Flickr API 测试脚本

3 thoughts on “Flickr API 测试脚本

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.