2011年11月22日火曜日
非公式APIをJAVAに移植してみた
Takehiro Takahashi氏による Google+ 非公式 投稿API for HSP を
JAVAに移植してみました
あくまでも非公式APIなのでGoogle先生の仕様変更によって
つかえなくなる可能性があります
再利用は自己責任でお願いします
package jp.kaientai.gplus;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.http.HttpResponse;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.NameValuePair;
import org.apache.http.protocol.HTTP;
public class Gplus {
private String _sendid;
private String _userid;
private DefaultHttpClient http;
public Gplus(String user,String pass) throws Exception {
http = new DefaultHttpClient();
Google_AccountLogin(user,pass);
}
private void Google_AccountLogin(String _mailaddress, String _password) throws Exception {
String b;
String c;
String d;
boolean blnMatch;
try {
HttpGet get = new HttpGet("https://accounts.google.com/login");
HttpResponse execute = http.execute(get);
InputStream is = execute.getEntity().getContent();
InputStreamReader isr = new InputStreamReader(is);
StringWriter sw = new StringWriter();
BufferedReader buf = new BufferedReader(isr);
for (String line; (line = buf.readLine()) != null;) {
sw.write(line);
}
b = sw.toString();
Pattern pattern = Pattern
.compile("name=\"dsh\" id=\"dsh\" value=\"([-0-9]+)\"");
Matcher matcher = pattern.matcher(b);
blnMatch = matcher.find();
c = matcher.group(1);
pattern = Pattern
.compile("name=\"GALX\"[ \t\n]+value=\"([-_a-zA-Z0-9]+)\"><");
matcher = pattern.matcher(b);
blnMatch = matcher.find();
if(!blnMatch )
{
throw new Exception("ログインエラー");
}
d = matcher.group(1);
HttpPost post = new HttpPost(
"https://accounts.google.com/ServiceLoginAuth");
post.setHeader("Content-Type", "application/x-www-form-urlencoded");
final List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("dsh", c));
params.add(new BasicNameValuePair("GALX", d));
params.add(new BasicNameValuePair("pstMsg", "1"));
params.add(new BasicNameValuePair("dnConn",
"https%3A%2F%2Faccounts.youtube.com"));
params.add(new BasicNameValuePair("timeStmp", ""));
params.add(new BasicNameValuePair("secTok", ""));
params.add(new BasicNameValuePair("Email", _mailaddress));
params.add(new BasicNameValuePair("Passwd", _password));
params.add(new BasicNameValuePair("signIn",
"%E3%83%AD%E3%82%B0%E3%82%A4%E3%83%B3"));
params.add(new BasicNameValuePair("PersistentCookie", "yes"));
params.add(new BasicNameValuePair("rmShown", "1"));
post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
execute = http.execute(post);
is = execute.getEntity().getContent();
isr = new InputStreamReader(is);
sw = new StringWriter();
buf = new BufferedReader(isr);
for (String line; (line = buf.readLine()) != null;) {
sw.write(line);
}
} catch (Exception e) {
e.printStackTrace();
}
return;
}
private void PlusGetData(String _pageid) {
boolean blnMatch;
HttpGet get = new HttpGet("https://plus.google.com/");
try {
get.setHeader("User-Agent",
"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)");
HttpResponse execute = http.execute(get);
InputStream is = execute.getEntity().getContent();
InputStreamReader isr = new InputStreamReader(is);
StringWriter sw = new StringWriter();
BufferedReader buf = new BufferedReader(isr);
for (String line; (line = buf.readLine()) != null;) {
sw.write(line);
}
String b = sw.toString();
Pattern pattern = Pattern.compile("\"(AObGSA.*:[0-9]*)\"");
Matcher matcher = pattern.matcher(b);
blnMatch = matcher.find();
_sendid = matcher.group(1);
pattern = Pattern.compile("key: '2', data:[ ]\\[\"([0-9]*)");
matcher = pattern.matcher(b);
blnMatch = matcher.find();
_userid = matcher.group(1);
} catch (Exception e) {
e.printStackTrace();
}
}
public int Google_Plus_Post(String _message, String _id, boolean _lock) {
String e;
String f;
String g;
try{
e = _message.replaceAll("\n", "\\\\n");
}catch(Exception ex)
{
ex.printStackTrace();
return 0;
}
PlusGetData(_id);
if (_lock) {
f = "true";
} else {
f = "false";
}
// JSON
if (_id == "") {
g = "[\""
+ e
+ "\",\"oz:"
+ _userid
+ "."
+ String.valueOf(System.currentTimeMillis() / 1000)
+ ".0\",null,null,null,null,\"[]\",null,\"{\\\"aclEntries\\\":[{\\\"scope\\\":{\\\"scopeType\\\":\\\"anyone\\\",\\\"name\\\":\\\"\\\\u5168\\\\u54e1\\\",\\\"id\\\":\\\"anyone\\\",\\\"me\\\":true,\\\"requiresKey\\\":false},\\\"role\\\":20},{\\\"scope\\\":{\\\"scopeType\\\":\\\"anyone\\\",\\\"name\\\":\\\"\\\\u5168\\\\u54e1\\\",\\\"id\\\":\\\"anyone\\\",\\\"me\\\":true,\\\"requiresKey\\\":false},\\\"role\\\":60}]}\",true,[],false,false,null,[],false,false,null,null,null,null,null,null,null,null,null,null,"
+ f + ",false,false]";
} else {
g = "[\""
+ e
+ "\",\"oz:"
+ _userid
+ "."
+ String.valueOf(System.currentTimeMillis() / 1000)
+ ".0\",null,null,null,null,\"[]\",null,\"{\\\"aclEntries\\\":[{\\\"scope\\\":{\\\"scopeType\\\":\\\"anyone\\\",\\\"name\\\":\\\"\\\\u5168\\\\u54e1\\\",\\\"id\\\":\\\"anyone\\\",\\\"me\\\":true,\\\"requiresKey\\\":false},\\\"role\\\":20},{\\\"scope\\\":{\\\"scopeType\\\":\\\"anyone\\\",\\\"name\\\":\\\"\\\\u5168\\\\u54e1\\\",\\\"id\\\":\\\"anyone\\\",\\\"me\\\":true,\\\"requiresKey\\\":false},\\\"role\\\":60}]}\",true,[],false,false,null,[],false,false,null,null,null,null,null,null,null,null,null,null,"
+ f + ",false,false]";
}
String url;
// 謚慕ィソ
// randomize
if (_id.equals("")) {
url = "https://plus.google.com/_/sharebox/post/";
} else {
url = "https://plus.google.com/b/" + _id + "/_/sharebox/post/";
}
HttpPost post = new HttpPost(url);
final List<NameValuePair> params = new ArrayList<NameValuePair>();
Random rnd = new Random();
try {
params.add(new BasicNameValuePair("spam", "20"));
params.add(new BasicNameValuePair("reqid",
String.valueOf(rnd.nextInt(10))
+ String.valueOf(rnd.nextInt(10))
+ String.valueOf(rnd.nextInt(10))
+ String.valueOf(rnd.nextInt(10))
+ String.valueOf(rnd.nextInt(10))
+ String.valueOf(rnd.nextInt(10))
+ String.valueOf(rnd.nextInt(10))));
params.add(new BasicNameValuePair("rt", "j"));
params.add(new BasicNameValuePair("spar", g));
params.add(new BasicNameValuePair("at", _sendid));
post.setHeader("X-Same-Domain", "1");
post.setHeader("Content-Type",
"application/x-www-form-urlencoded;charset=utf-8");
post.setHeader("Pragma", "no-cache");
post.setHeader("Cache-Control", "no-cache");
post.setHeader("User-Agent",
"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)");
post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
HttpResponse execute = http.execute(post);
InputStream is = execute.getEntity().getContent();
InputStreamReader isr = new InputStreamReader(is);
StringWriter sw = new StringWriter();
BufferedReader buf = new BufferedReader(isr);
for (String line; (line = buf.readLine()) != null;) {
sw.write(line);
}
System.out.println( sw.toString());
if (execute != null) {
return 1;
} else {
return 0;
}
} catch (Exception ex) {
ex.printStackTrace();
}
return 0;
}
//Google+の指定のポストを編集 _postid:ページID, _message:編集後の本文 _id: ページの場合
public int Google_Plus_Edit( String _postid, String _message, String _id)
{
String e;
String url;
try{
e = _message.replaceAll("\n", "\\\n");
}catch(Exception ex)
{
ex.printStackTrace();
return 0;
}
PlusGetData(_id);
if (_id.equals("")) {
url = "https://plus.google.com/_/stream/edit/";
} else {
url = "https://plus.google.com/b/"+ _id + "/_/stream/edit/";
}
HttpPost post = new HttpPost(url);
final List<NameValuePair> params = new ArrayList<NameValuePair>();
Random rnd = new Random();
try {
params.add(new BasicNameValuePair("spam", "20"));
params.add(new BasicNameValuePair("reqid",
String.valueOf(rnd.nextInt(10))
+ String.valueOf(rnd.nextInt(10))
+ String.valueOf(rnd.nextInt(10))
+ String.valueOf(rnd.nextInt(10))
+ String.valueOf(rnd.nextInt(10))
+ String.valueOf(rnd.nextInt(10))
+ String.valueOf(rnd.nextInt(10))));
params.add(new BasicNameValuePair("rt", "j"));
params.add(new BasicNameValuePair("spt" ,e));
params.add(new BasicNameValuePair("sei" ,_postid));
params.add(new BasicNameValuePair("sml","[]"));
params.add(new BasicNameValuePair("madl","true"));
params.add(new BasicNameValuePair("at", _sendid));
post.setHeader("X-Same-Domain", "1");
post.setHeader("Content-Type",
"application/x-www-form-urlencoded;charset=utf-8");
post.setHeader("Pragma", "no-cache");
post.setHeader("Cache-Control", "no-cache");
post.setHeader("User-Agent",
"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)");
post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
HttpResponse execute = http.execute(post);
InputStream is = execute.getEntity().getContent();
InputStreamReader isr = new InputStreamReader(is);
StringWriter sw = new StringWriter();
BufferedReader buf = new BufferedReader(isr);
for (String line; (line = buf.readLine()) != null;) {
sw.write(line);
}
System.out.println( sw.toString());
if (execute != null) {
return 1;
} else {
return 0;
}
} catch (Exception ex) {
ex.printStackTrace();
}
return 0;
}
public static void main(String[] args) {
try{
Gplus g = new Gplus("アカウント", "パスワード");
g.Google_Plus_Post("てすと\n改行", "", true);
g.Google_Plus_Edit("POSTID", "編集は\n甘え", "");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
登録:
コメントの投稿 (Atom)
0 件のコメント:
コメントを投稿