2

我有一个Android项目,允许我log-in with Google-accounts。一切工作,因为它应该和我能够检索人的(com.google.android.gsm.model.people.Person)数据,如谷歌的电子邮件,用户名,个人资料图片网址等。Android - Google登录Web API - 如何发送Google Sign-in POST请求?

我也有一个Web API托管在线。在这个Web API中,我可以使用以下代码获取产品的JSON列表:mywebhost/api/products

显然,这些GET-请求都由OAuth 2.0用户授权,这意味着我必须登录与谷歌帐户的mywebhost/Account/Login页面上被授权从Web API的JSON列表。 (当我没有登录时,我收到以下JSON列表:{"$id":"1","Message":"Authorization has been denied for this request."}

我知道如何在Android中发送POST请求。例如与代码:

public class TaskPostAPI extends AsyncTask<String, Void, String> 
{ 
    GoogleApiClient googleAPI; 

    public TaskPostAPI(GoogleApiClient googleAPI){ 
     this.googleAPI = googleAPI; 
    } 

    @Override 
    protected String doInBackground(String... urls){ 
     String response = ""; 
     for(String url : urls){ 
      DefaultHttpClient client = new DefaultHttpClient(); 
      HttpPost post = new HttpPost(url); 
      try{ 
       List<NameValuePair> nvPairs = new ArrayList<NameValuePair>(3); 
       //nvPairs.add(new BasicNameValuePair("personName", Plus.PeopleApi.getCurrentPerson(googleAPI).getDisplayName())); 
       //nvPairs.add(new BasicNameValuePair("personGooglePlusProfile", Plus.PeopleApi.getCurrentPerson(googleAPI).getUrl())); 
       //nvPairs.add(new BasicNameValuePair("personEmail", Plus.AccountApi.getAccountName(googleAPI))); 

       // TODO: Use the correct nvPairs to be able to Log-in with the Google-account 

       post.setEntity(new UrlEncodedFormEntity(nvPairs)); 

       HttpResponse execute = client.execute(post); 
       InputStream content = execute.getEntity().getContent(); 

       BufferedReader buffer = new BufferedReader(new InputStreamReader(content)); 
       String s = ""; 
       while((s = buffer.readLine()) != null) 
        response += s; 
      } 
      catch(Exception ex){ 
       ex.printStackTrace(); 
      } 
     } 
     return response; 
    } 

    @Override 
    protected void onPostExecute(String result){ 
     // Do nothing yet 
    } 
} 

所以,现在的问题是:

  1. 在上面的代码示例,如果nvPairs是什么能够成功登录与谷歌账户。或者我应该使用与普通HttpPost完全不同的内容在我的Web API上使用Google帐户登录?
  2. 此外:提供的网址是默认的登录页面。在这个页面上,我有三种不同的登录选项。由于我们只想使用Google登录功能,因此如何从Web API登录页面检索Google登录按钮的URL以用于POST请求?

2b。第二个问题:我可以在FireFox中使用什么插件来查看我重定向到的链接?所以我知道应该使用哪个登录URL,而不是默认的登录页面。

在此先感谢您的回复。


编辑1:我试过了不同的方法,但我不确定它会为HttpGet请求工作。我试过的是使用登录页面打开WebView,当我成功登录后到达页面后,关闭WebView。

但是,当我使用HttpGet请求时,我仍然收到未授权的JSON,那么如何使用此WebView登录来使HttpGet请求“相信”我已登录并授权?

如果有人仍然有使用第一种方法(HttpPost-request)的想法,或者如果某人有完全不同的方法,请告诉我。

回答

0

好的,我找到了我可以在C#web项目(/POST/ExternalLogin)中使用的POST。在那里,我也知道我应该送什么:

header

  • 内容类型application/x-WWW窗体-urlencoded
  • 饼干与__RequestVerificationToken

body

  • provider(“Google”)
  • RETURNURL
  • __RequestVerificationToken

第二__RequestVerificationToken需要是不同的一个比在Cookie中使用的,但C#网页API在解密之后它应该是相同的。这是我现在唯一的问题,但我有一个不同的stackoverflow问题。

对于全码:

public class TaskPostAPI extends AsyncTask<String, Void, String> 
{ 
    private String TOKEN = "__RequestVerificationToken"; 

    @Override 
    protected String doInBackground(String... urls){ 
     String response = ""; 
     for(String url : urls){ 
      HttpPost post = new HttpPost(url); 
      try{ 
       // Add the default Content-type to the Header 
       post.addHeader("Content-type", "application/x-www-form-urlencoded"); 

       // Get the baseUrl from the given url 
       URL u = new URL(url); 
       String baseUrl = u.getProtocol() + "://" + u.getHost(); 

       // POST-request requires anti-forgery Cookie 
       // Get all Cookies 
       CookieManager cookieManager = CookieManager.getInstance(); 
       String cookie = cookieManager.getCookie(baseUrl); 
       String[] cookies = cookie.split(";"); 

       // Put all Cookies in a HashMap with cookieKey & cookieToken 
       HashMap<String, String> cookieStrings = new HashMap<String, String>(); 
       for(String cook : cookies){ 
        String[] cs = cook.split("="); 
        cookieStrings.put(cs[0], cs[1]); 
       } 

       // Add the Cookie to the Header 
       post.addHeader("Cookie", TOKEN + "=" + cookieStrings.get(TOKEN) + ""); 

       // POST-request requires cookieToken, provider and returnUrl 
       List<NameValuePair> nvPairs = new ArrayList<NameValuePair>(3); 
       nvPairs.add(new BasicNameValuePair(TOKEN, cookieStrings.get(TOKEN))); 
       nvPairs.add(new BasicNameValuePair("provider", "Google")); 
       nvPairs.add(new BasicNameValuePair("returnUrl", baseUrl)); 
       post.setEntity(new UrlEncodedFormEntity(nvPairs)); 

       Log.i("COOKIE OUTPUT", TOKEN + "=" + cookieStrings.get(TOKEN) + ""); 

       // Send the POST-request 
       HttpResponse execute = MainActivity.HttpClient.execute(post); 

       // Get the response of the POST-request 
       InputStream content = execute.getEntity().getContent(); 
       BufferedReader buffer = new BufferedReader(new InputStreamReader(content)); 
       String s = ""; 
       while((s = buffer.readLine()) != null) 
        response += s; 
      } 
      catch(Exception ex){ 
       ex.printStackTrace(); 
      } 
     } 
     return response; 
    } 

在这片下面一行代码是不正确的,仍然需要修理:

nvPairs.add(new BasicNameValuePair(TOKEN, cookieStrings.get(TOKEN)));

cookieStrings.get(TOKEN)需要以正确的令牌将被替换发送。