Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android-谷歌登录Web API-如何发送谷歌登录后请求?_Android_Asp.net Web Api_Authorization_Google Api Client_Google Login - Fatal编程技术网

Android-谷歌登录Web API-如何发送谷歌登录后请求?

Android-谷歌登录Web API-如何发送谷歌登录后请求?,android,asp.net-web-api,authorization,google-api-client,google-login,Android,Asp.net Web Api,Authorization,Google Api Client,Google Login,我有一个Android项目,可以让我。一切正常,我能够检索此人的(com.google.android.gsm.model.people.Person)数据,如谷歌电子邮件、用户名、个人资料图片url等 我还有一个在线托管的Web API。在这个Web API中,我可以通过使用以下内容获得产品的JSON列表:mywebhost/API/products 显然,这些GET请求是由OAuth 2.0授权的,这意味着我必须使用mywebhost/Account/Login页面上的Google帐户登录,

我有一个Android项目,可以让我。一切正常,我能够检索此人的(
com.google.android.gsm.model.people.Person
)数据,如谷歌电子邮件、用户名、个人资料图片url等

我还有一个在线托管的Web API。在这个Web API中,我可以通过使用以下内容获得产品的JSON列表:
mywebhost/API/products

显然,这些GET请求是由OAuth 2.0授权的,这意味着我必须使用
mywebhost/Account/Login
页面上的Google帐户登录,才能获得从Web API获取JSON列表的授权。(未登录时,我收到以下JSON列表:
{“$id”:“1”,“消息”:“此请求的授权已被拒绝”。}

我知道如何在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
    }
}
公共类TaskPostAPI扩展了异步任务
{   
GoogleapClient googleAPI;
公共任务PostAPI(GoogleAppClient googleAPI){
this.googleAPI=googleAPI;
}
@凌驾
受保护的字符串doInBackground(字符串…URL){
字符串响应=”;
for(字符串url:url){
DefaultHttpClient=新的DefaultHttpClient();
HttpPost=新的HttpPost(url);
试一试{
列表nvPairs=新的ArrayList(3);
//添加(新的BasicNameValuePair(“personName”,Plus.PeopleApi.getCurrentPerson(googleAPI.getDisplayName()));
//添加(新的BasicNameValuePair(“personGooglePlusProfile”,Plus.PeopleApi.getCurrentPerson(googleAPI.getUrl()));
//添加(新的BasicNameValuePair(“personEmail”,Plus.AccountApi.getAccountName(googleAPI));
//TODO:使用正确的nvPairs可以使用Google帐户登录
post.setEntity(新的UrlEncodedFormEntity(nvPairs));
HttpResponse execute=client.execute(post);
InputStream内容=execute.getEntity().getContent();
BufferedReader buffer=新的BufferedReader(新的InputStreamReader(内容));
字符串s=“”;
而((s=buffer.readLine())!=null)
响应+=s;
}
捕获(例外情况除外){
例如printStackTrace();
}
}
返回响应;
}
@凌驾
受保护的void onPostExecute(字符串结果){
//什么也不做
}
}
因此,现在的问题是:

  • 在上面的代码示例中,nvPairs应该具备哪些条件才能使用Google帐户成功登录。或者我应该使用与普通HttpPost完全不同的东西在我的Web API上使用Google帐户登录吗
  • 另外:提供的url是默认的登录页面。在这个页面上,我有三个不同的登录选项。既然我们只想使用Google登录,那么如何从Web API登录页面检索Google登录按钮的url以用于POST请求
  • 2b。关于第二个问题:我可以在FireFox中使用什么插件来查看我重定向到的链接?所以我知道应该使用哪个登录url而不是默认的登录页面

    提前感谢您的回复


    编辑1:我尝试了另一种方法,但我不确定它是否适用于HttpGet请求。我尝试的是用登录页面打开一个WebView,当我到达成功登录的页面后,我关闭WebView

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


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

    好的,我已经找到了可以在C#web项目中使用的
    POST
    /POST/ExternalLogin
    )。在那里,我也看到了我应该发送的内容:

    标题中

    • 内容类型应用程序/x-www-form-urlencoded
    • 带有_RequestVerificationToken的Cookie
    正文中

    • 供应商(“谷歌”)
    • 返回URL
    • __请求验证登录
    第二个u RequestVerificationToken需要与Cookie中使用的不同,但在C#Web 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;
        }
    
    公共类TaskPostAPI扩展了异步任务
    {
    私有字符串令牌=“\uuu RequestVerificationToken”;
    @凌驾
    受保护的字符串doInBackground(字符串…URL){
    字符串响应=”;
    for(字符串url:url){
    HttpPost=新的HttpPost(url);
    试一试{
    //将默认内容类型添加到标题
    post.addHeader(“内容类型”、“应用程序/x-www-form-urlencoded”);
    //从给定的url获取baseUrl
    URL u=新的URL(URL);
    字符串baseUrl=u.getProtocol()+“:/”+u.getHost();
    //POST请求需要防伪Cookie
    //得到所有的饼干
    CookieManager CookieManager=CookieManager.getInstance();
    字符串cookie=cookieManager.getCookie(baseUrl);
    字符串[]cookies=cookie.split(“;”);
    //使用cookieKey&cookieToken将所有Cookie放入HashMap中
    HashMap cookieStrings=新HashMap();
    用于(串厨师:饼干){
    字符串[]cs=cook.split(“”);
    cookieStrings.put(cs[0],cs[1]);
    }
    //将Cookie添加到标头
    post.addHeader(“Cookie”,TOKEN+“=”+cookieStrings.get(TOKEN)+”);
    //POST请求要求