2012-09-27 160 views
0
https://developers.google.com/accounts/docs/OAuth2InstalledApp 

我給用戶提供谷歌帳戶上申請在網頁視圖與下面的鏈接註冊使用獲得授權碼

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和重定向URI,隨着谷歌Android的驗證2.0 HTTP POST請求每通過谷歌API控制檯給出即選擇一個重定向URI https://developers.google.com/accounts/docs/OAuth2InstalledApp

終於得到了在瀏覽器的標題欄使用view.getTitle()

0123返回的授權碼

之後另一個請求需要發送 實際要求可能看起來像:

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(); 
    } 

所以我越來越壞令牌響應.. 。我從昨天開始嘗試這個,並建議和幫助,將不勝感激。我的主要目標是獲取用戶信息使用gmail帳戶在Android

+0

只是爲了確保:您每次都會生成一個新的授權碼,而此處的硬編碼碼僅用於演示目的? –

+1

是啊,我總是生成一個新鮮的......並在我的webview頁面完成後調用此方法.....即onPageFinished(WebView視圖,字符串url) –

+0

而且你還添加'client_secret','redirect_uri' ,'grant_type'爲'a',對吧?然後我沒有看到你的代碼有什麼明顯的錯誤。您也許可以嘗試最近推出的[Google Play服務](http://android-developers.blogspot.de/2012/09/google-play-services-and-oauth-identity.html)來處理OAuth(也適用於更安全的方式)爲你。一旦獲得訪問令牌,您就可以使用Google的[OAuth2 API庫](https://code.google.com/p/google-api-java-client/wiki/APIs#oauth2)獲取「 Userinfo'對象。 –

回答

1

我覺得你在這裏混合了不同的流了一下:

  • Client-side flow要求客戶端機密,但主要是爲了JavaScript應用程序。
  • Installed Applications flow確實需要客戶端機密,雖然:

    的CLIENT_ID和嵌入在應用程序的源代碼,註冊時client_secret獲得。在這種情況下,client_secret顯然不被視爲祕密。

    你可能在API console安裝的應用程序生成的客戶端ID - > Android的,所以你只得到了客戶端ID,不得不指定應用程序的證書指紋。這種類型的客戶端ID是用於最近發佈和推薦使用(因爲它更安全)Google Play Services

    如果你想使用安裝的應用程序手動流程,你必須生成一個客戶端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 
    
+0

那麼我如何獲得client_secret ID和grant_type爲已安裝的應用程序ie在Android中,我只有API控制檯中給出的client_id和redirect_uri。 –

+0

點擊「創建另一個客戶端ID」,選中「已安裝的應用程序」,選中「其他」,點擊「創建客戶端ID」。然後你得到一個新的'client_id'和'client_secret'。 'grant_type'必須是固定值'authorization_code'。 –

+0

謝謝我會嘗試,但client_id不會引用已安裝的應用程序,它只適用於各個客戶端的網絡應用程序client_secret –

0

我終於找到工作樣例 您可以使用Google+開發者API。看看這個項目github和這篇文章。 here