나는 Android 앱의 백엔드 웹 서비스 개발자입니다. Google은 구독으로 앱에 서비스를 제공하고 있으며 구독 구매를 위해 Google Play를 사용할 것입니다. Google은 앱에서 사용자가 사용할 수있는 서비스를 추적하기위한 백엔드 라이선스 서버를 보유하고 있습니다.백엔드 서비스에서 OAuth2 액세스 및 새로 고침 토큰
Google은 특정 이유로 백엔드에서 구매에 대한 구독 상태를 얻고 싶습니다. 하지만 Google의 OAuth2 웹 서비스에서 액세스 및 새로 고침 토큰을 가져 오는 데 문제가 있습니다. 내가 설정 클라이언트 ID를 사용하는 Google 계정에 직접 액세스 할 수없는 Google Play Android Developer API
와 나는 단계에 대한 작업을 수행하는 액세스 권한이있는 관리자를했다 :
나는 다음 페이지에서 지침을 따랐다 초기 코드를 얻기 위해 웹 호출을 수행하십시오. 그런 다음 HTTP POST를 사용하여 권한 부여 인증을 호출하는 데 사용하지만 "invalid_grant"오류 만 포함하는 Json으로 HTTP 400을 가져옵니다.
string postData = String.Format("code={0}&client_id={1}&client_secret={2}&redirect_uri={3}&grant_type=authorization_code",
oauthCode,
clientId,
clientSecret,
redirectURL
);
byte[] data = Encoding.UTF8.GetBytes(postData);
try
{
HttpWebRequest wreq = (HttpWebRequest)WebRequest.Create("https://accounts.google.com/o/oauth2/token");
wreq.Method = "POST";
wreq.ContentType = "application/x-www-form-urlencoded";
wreq.ContentLength = data.Length;
wreq.Accept = "application/json";
using (Stream wstream = wreq.GetRequestStream())
{
wstream.Write(data, 0, data.Length);
}
HttpWebResponse wresp = (HttpWebResponse)wreq.GetResponse();
if (wresp.StatusCode == HttpStatusCode.OK)
{
using (Stream rstream = wresp.GetResponseStream())
{
StringBuilder jresp = new StringBuilder();
byte[] buffer = new byte[512];
int read = 0;
while ((read = rstream.Read(buffer, 0, buffer.Length)) > 0)
jresp.Append(Encoding.UTF8.GetString(buffer, 0, read));
Console.WriteLine(jresp.ToString());
}
}
else
Console.WriteLine("damn...");
}
catch (WebException wex)
{
Console.WriteLine(wex.ToString());
Console.WriteLine("================================");
if (wex.Response != null)
{
StringBuilder inner = new StringBuilder();
byte[] buffer = new byte[512];
int read = 0;
using (Stream xstream = wex.Response.GetResponseStream())
{
while ((read = xstream.Read(buffer, 0, buffer.Length)) > 0)
inner.Append(Encoding.UTF8.GetString(buffer, 0, read));
}
Console.WriteLine(inner.ToString());
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
내가 초기 코드 인증 웹 브라우저 호출이 아닌 다른 위치에서 그것을 할 수 없을 것이라고 걱정하고 해당 액세스/새로 고침 토큰이 할 수있는 다음은 POST 코드의 예입니다 웹 브라우저 경험에서 백엔드로 수행 할 수 없습니다.
코드에 무엇이 문제인지 파악하기 위해 액세스 권한이있는 Google 계정을 사용하지 않는 이유는 무엇입니까? 마치 코드를 좀 더 디버깅해야 할 것 같은데. –
Google 계정에 액세스 할 수 없으며 특정 계정으로 작업해야합니다. 그래서 내가 먼저 묻습니다. 코드에 대해 아무런 이슈가 보이지 않기 때문에, 누군가 프로세스에서 경험이 있고 실제 포인터를 줄 수 있는지 다시 묻습니다. 예를 들어, 왜 우리가 그것을하고있는 방식으로 작동하지 않을 수 있습니다. – Quintium