我在Google Play中销售Android应用,所有订单照常通过Google电子钱包。 Google电子钱包不允许对订单列表进行分组和过滤,因此我想创建一个小型实用程序(C#,WinForms)以更方便的方式显示订单列表。我设法找到以下网址,允许下载包含订单信息的CSV文件,此文件完全符合我的要求" https://wallet.google.com/merchant/pages/u/0/bcid-XXX/oid-YYY/cid-ZZZ/purchaseorderdownload?startTime=...&endTime= ..."。但是,我无法向Google授权,我无法在未重定向到登录页面的情况下使用此URL。由于我要创建一个WinForms工具,我无法使用此登录页面。那么,问题是,如何以某种自动模式向Google授权,以便我可以使用此URL并下载CSV文件以便在我的工具中进一步处理?
答案 0 :(得分:0)
特德,这是我的代码:
private const string LoginUrl = "https://accounts.google.com/ServiceLoginAuth";
private const string WalletUrl = "https://wallet.google.com/merchant/pages";
private readonly DateTime _unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
private const string Username = "YourUserName@gmail.com";
private const string Password = "YourPassword";
private readonly DateTime _startDate = new DateTime(2014, 9, 1);
private readonly DateTime _endDate = new DateTime(2014, 9, 30);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string orders = GetLoginHtml();
}
private string GetLoginHtml()
{
var request = (HttpWebRequest)WebRequest.Create(LoginUrl);
var cookieJar = new CookieContainer();
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.CookieContainer = cookieJar;
using (var requestStream = request.GetRequestStream())
{
string content = "Email=" + Username + "&Passwd=" + Password;
requestStream.Write(Encoding.UTF8.GetBytes(content), 0, Encoding.UTF8.GetBytes(content).Length);
using (var sr = new StreamReader(request.GetResponse().GetResponseStream()))
{
string html = sr.ReadToEnd();
string galxValue = ParseOutValue(html, "GALX");
return GetLoginHtml2(galxValue, cookieJar);
}
}
}
private string GetLoginHtml2(string galx, CookieContainer cookieJar)
{
var request = (HttpWebRequest)WebRequest.Create(LoginUrl);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.CookieContainer = cookieJar;
using (var requestStream = request.GetRequestStream())
{
string content = "Email=" + Username + "&Passwd=" + Password + "&GALX=" + galx;
requestStream.Write(Encoding.UTF8.GetBytes(content), 0, Encoding.UTF8.GetBytes(content).Length);
using (var sr = new StreamReader(request.GetResponse().GetResponseStream()))
{
string html = sr.ReadToEnd();
return GetLoginHtml3(galx, cookieJar);
}
}
}
private string GetLoginHtml3(string galx, CookieContainer cookieJar)
{
var request = (HttpWebRequest)WebRequest.Create(WalletUrl);
request.Method = "GET";
request.ContentType = "text/xml";
request.CookieContainer = cookieJar;
using (var sr = new StreamReader(request.GetResponse().GetResponseStream()))
{
string html = sr.ReadToEnd();
string bcid = ParseOutBcid(html);
string oid = ParseOutOid(html);
string cid = ParseOutCid(html);
string orders = GetOrders(cookieJar, bcid, oid, cid, _startDate, _endDate);
return orders;
}
}
private string GetOrders(CookieContainer cookieJar, string bcid, string oid, string cid, DateTime startDate, DateTime endDate)
{
var st = (long)(startDate.ToUniversalTime() - _unixEpoch).TotalMilliseconds;
var et = (long)(endDate.ToUniversalTime() - _unixEpoch).TotalMilliseconds;
var request = (HttpWebRequest)WebRequest.Create(WalletUrl + "/u/0/bcid-" + bcid + "/oid-" + oid + "/cid-" + cid + "/purchaseorderdownload?startTime=" + st + "&endTime=" + et);
request.Method = "GET";
request.ContentType = "text/xml";
request.CookieContainer = cookieJar;
using (var sr = new StreamReader(request.GetResponse().GetResponseStream()))
{
string html = sr.ReadToEnd();
return html;
}
}
private string ParseOutValue(string html, string value)
{
int ndx1 = html.IndexOf("<input name=\"" + value + "\"", StringComparison.Ordinal);
int ndx2 = html.IndexOf("value=", ndx1, StringComparison.Ordinal);
return html.Substring(ndx2 + 7, html.IndexOf("\"", ndx2 + 7, StringComparison.Ordinal) - ndx2 - 7);
}
private string ParseOutBcid(string html)
{
int ndx1 = html.IndexOf("bcid-", StringComparison.Ordinal);
int ndx2 = html.IndexOf("/oid", ndx1, StringComparison.Ordinal);
return html.Substring(ndx1 + 5, ndx2 - ndx1 - 5);
}
private string ParseOutOid(string html)
{
int ndx1 = html.IndexOf("/oid-", StringComparison.Ordinal);
int ndx2 = html.IndexOf("/cid", ndx1, StringComparison.Ordinal);
return html.Substring(ndx1 + 5, ndx2 - ndx1 - 5);
}
private string ParseOutCid(string html)
{
int ndx1 = html.IndexOf("/cid-", StringComparison.Ordinal);
string retval = "";
ndx1 = ndx1 + 5;
while (char.IsNumber(html[ndx1]))
{
retval = retval + html[ndx1];
ndx1++;
}
return retval;
}
有几件事。首先,您需要将用户名更改为您的Gmail用户名,并将密码更改为您的密码。此外,将_startDate和_endDate设置为您尝试提取的任何时间范围。你不应该改变任何其他东西。只需调用“GetLoginHtml()”函数,它就会返回您的电子钱包订单!
请记住,我还没有机会优化代码。它很原始,可能仍然有bug。另外,另外一件事是,如果您在运行时遇到索引错误,那么因为您必须首次打开Internet Explorer并登录Google帐户。登录后,您可以关闭Internet Explorer,之后程序应该可以正常工作。我需要想办法解决这个问题,我还没有。
让我知道你的想法!