2013-04-14 98 views
0

我是JAVA新手,使用play framework創建網站。 我正在嘗試將Facebook oauth集成到我的網站中。我有代碼是:Facebook OAuth異常:redirect_uri缺失

// This function is called to populate _facebook, _facebookAuthParams and 
// _facebookTokenParams. The first two are HashMaps and _facebookTokenParams is 
// ObjectNode 
private static void _initFacebook() { 

    String _url = "shutterdeck.com:8080"; 

    _facebook.put("authUrl", "https://graph.facebook.com/oauth/authorize"); 
    _facebook.put("tokenUrl", "https://graph.facebook.com/oauth/access_token"); 
    _facebookAuthParams.put("scope", "email"); 
    _facebookAuthParams.put("responseType", "code"); 
    _facebookAuthParams.put("redirect_uri", _url + "/oauth/facebookRedirect"); 
    _facebookAuthParams.put("client_id", ".."); 
    _facebookTokenParams.put("grant_type", "authorization_code"); 
    _facebookTokenParams.put("redirect_uri", _url + "/oauth/facebookRedirect"); 
    _facebookTokenParams.put("scope", "email"); 
    _facebookTokenParams.put("client_id", "..."); 
    _facebookTokenParams.put("client_secret", "..."); 
} 

了以下功能試圖從Facebook獲取的access_token:

public static Result facebookRedirect() { 
    Map<String, String[]> query = request().queryString(); 
    if (query.containsKey("code")) { 
     String[] value = query.get("code"); 
     String code = value[0]; 
     if (value.length > 1 || code.isEmpty()) 
      return redirect("/"); 

     String tokenUrl = _getTokenUrl(_facebook); 
     _facebookTokenParams.put("code", code); 
     Promise<WS.Response> promise = WS.url(tokenUrl).post(_facebookTokenParams); 
     Function<WS.Response, Result> f = new Function<WS.Response, Result>() { 
      public Result apply(WS.Response response) { 
        System.out.println(response.asJson()); // This prints an error json from face book 
        String token = response.asJson().get("token").asText(); 
        _getFacebookUserInfo(token); 
        return ok("/"); 
      } 
     }; 
     return async(promise.map(f)); 
    } 
    return ok("404"); 
} 

說我從Facebook得到的錯誤是:

{"error":{"message":"Missing redirect_uri parameter.","type":"OAuthException","code":191}} 

我驗證了redirect_url存在於_facebookTokenParams中,並傳遞給post。我在這裏錯過了什麼?此外,我如何查看發送到Facebook的POST request

回答

0

您必須在查詢字符串中傳遞參數。

1

由於某些原因,Facebook Graph API端點希望我們將URL中的client_id,client_secret和grant_type參數作爲url編碼參數傳遞。這意味着grant_type上沒有空格 - 只是證書之間的一個逗號!