我有一個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
}
}
所以,現在的問題是:
- 在上面的代碼示例,如果nvPairs是什麼能夠成功登錄與谷歌賬戶。或者我應該使用與普通HttpPost完全不同的內容在我的Web API上使用Google帳戶登錄?
- 此外:提供的網址是默認的登錄頁面。在這個頁面上,我有三種不同的登錄選項。由於我們只想使用Google登錄功能,因此如何從Web API登錄頁面檢索Google登錄按鈕的URL以用於POST請求?
2b。第二個問題:我可以在FireFox中使用什麼插件來查看我重定向到的鏈接?所以我知道應該使用哪個登錄URL,而不是默認的登錄頁面。
在此先感謝您的回覆。
編輯1:我試過了不同的方法,但我不確定它會爲HttpGet請求工作。我試過的是使用登錄頁面打開WebView,當我成功登錄後到達頁面後,關閉WebView。
但是,當我使用HttpGet請求時,我仍然收到未授權的JSON,那麼如何使用此WebView登錄來使HttpGet請求「相信」我已登錄並授權?
如果有人仍然有使用第一種方法(HttpPost-request)的想法,或者如果某人有完全不同的方法,請告訴我。