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)需要以正確的令牌將被替換髮送。