簡體   English   中英

獲取授權碼后使用Google Android Auth 2.0 HTTP發布請求進行注冊

[英]SignUp using Google Android Auth 2.0 HTTP post request after getting authorization code

https://developers.google.com/accounts/docs/OAuth2InstalledApp

我通過以下鏈接為用戶提供在Webview中使用Google帳戶注冊的權限

webview.loadUrl("https://accounts.google.com/o/oauth2/auth?scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile&state=%2F&response_type=code&redirect_uri=urn:ietf:wg:oauth:2.0:oob&client_id=706645665586.apps.googleusercontent.com");

其中包含我的客戶ID和Google API控制台指定的重定向URI,即選擇重定向URI https://developers.google.com/accounts/docs/OAuth2InstalledApp

最后,使用view.getTitle()獲取在瀏覽器標題欄中返回的授權代碼。

之后,需要發送另一個請求。實際的請求可能如下所示:

POST /o/oauth2/token HTTP/1.1
Host: accounts.google.com
Content-Type: application/x-www-form-urlencoded
code=4/y_jtre05wvb6QSPo0Tkx5AbLfWB
client_id=706645665586.apps.googleusercontent.com
client_secret={client_secret}&
redirect_uri=urn:ietf:wg:oauth:2.0:oob
grant_type=authorization_code

所以現在在發出HTTP POST請求時..

    DefaultHttpClient httpcl = new DefaultHttpClient();
    HttpPost httpp = new HttpPost("https://accounts.google.com/o/oauth2/auth");
    List<NameValuePair> a = new ArrayList<NameValuePair>();
    a.add(new BasicNameValuePair("code", "4/y_jtre05wvb6QSPo0Tkx5AbLfWB"));
    a.add(new BasicNameValuePair("client_id", "706645665586.apps.googleusercontent.com"));
    try {
        StringEntity mEntity = new StringEntity("");
        mEntity.setContentType(" application/x-www-form-urlencoded");
        httpp.setEntity(mEntity);
        httpp.setEntity(new UrlEncodedFormEntity(a));
        HttpResponse response1 = httpcl.execute(httpp);

        String response = EntityUtils.toString(response1.getEntity());
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

所以我收到了錯誤令牌響應...我從昨天開始嘗試使用它,建議和幫助將不勝感激.. 我的主要目的是在Android中使用gmail帳戶獲取用戶信息

我認為您在這里混合了不同的流程:

  • 客戶端流程 要求客戶端機密 ,但主要是為了JavaScript應用程序。
  • 但是,“ 已安裝的應用程序”流 確實需要客戶端密鑰

    注冊期間獲得的client_id和client_secret嵌入在應用程序的源代碼中。 在這種情況下,client_secret顯然不會被視為秘密。

    您可能在API控制台中為“ 已安裝的應用程序-> Android”生成了一個客戶端ID ,因此您只有一個客戶端ID,並且必須指定應用程序的證書指紋 此類客戶端ID適用於最近發布和推薦(因為它更安全)的Google Play服務

    如果要手動使用“ 已安裝的應用程序”流 ,則必須為“ 已安裝的應用程序”->“其他”生成一個客戶機ID ,在此您還將獲得一個客戶機密鑰 在將授權代碼交換為訪問令牌時 ,然后需要指定所有五個參數:

     code The authorization code returned from the initial request client_id The client_id obtained during application registration client_secret The client secret obtained during application registration redirect_uri The URI registered with the application grant_type As defined in the OAuth 2.0 specification, this field must contain a value of authorization_code 

我終於找到了可以使用Google+開發人員API的工作示例。 github和本文上查看這個項目。 這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM