Search in sources :

Example 1 with OAuthRequest

use of com.funstill.kelefun.http.OAuthRequest in project keleFanfou by kelefun.

the class OAuthUtil method extractHeader.

/**
 * 普通接口请求
 *
 * 如果请求存在Authorization的Header并且以OAuth开头,则其随后的参数, 所有的名字以oauth_ 和xauth_(用于XAuth)开头的参数都参与签名
 * 如果请求是GET, 则QueryString中所有的参数都参与签名
 * 如果请求是POST并且,Content-Type是application/x-www-form-urlencoded, 则所有的POST参数需要参加签名
 * 其他的POST请求,参数都不参加签名,包括multipart/form-data
 * @param request
 * @return
 */
public String extractHeader(Request request) {
    // 获取请求参数
    HttpUrl url = request.url();
    OAuthRequest oAuthRequest = new OAuthRequest();
    // post请求--FormUrlEncoded
    RequestBody rb = request.body();
    if (rb != null && rb instanceof FormBody) {
        FormBody fb = (FormBody) rb;
        for (int i = 0; i < fb.size(); i++) {
            // oAuthRequest.setOauthParameter(fb.encodedName(i),fb.encodedValue(i));//工具类OAuthUtil会统一编码
            oAuthRequest.setOauthParameter(fb.name(i), fb.value(i));
        }
    }
    // get请求
    for (int i = 0; i < url.querySize(); i++) {
        oAuthRequest.setOauthParameter(url.queryParameterName(i), url.queryParameterValue(i));
    }
    oAuthRequest.setVerb(request.method());
    oAuthRequest.setBaseUrl(extractBaseUrl(url));
    // oauth
    addOAuthParams(oAuthRequest);
    return extractHeader(oAuthRequest);
}
Also used : OAuthRequest(com.funstill.kelefun.http.OAuthRequest) FormBody(okhttp3.FormBody) HttpUrl(okhttp3.HttpUrl) RequestBody(okhttp3.RequestBody)

Example 2 with OAuthRequest

use of com.funstill.kelefun.http.OAuthRequest in project keleFanfou by kelefun.

the class OAuthUtil method extractHeader.

/**
 * 通过用户账号密码请求access_token
 *
 * @param request
 * @param username
 * @param password
 * @return
 */
public String extractHeader(Request request, String username, String password) {
    HttpUrl url = request.url();
    OAuthRequest oAuthRequest = new OAuthRequest();
    for (int i = 0; i < url.querySize(); i++) {
        oAuthRequest.setOauthParameter(url.queryParameterName(i), url.queryParameterValue(i));
    }
    oAuthRequest.setVerb(request.method());
    oAuthRequest.setBaseUrl(extractBaseUrl(url));
    // xauth
    addXAuthParams(oAuthRequest, username, password);
    return extractHeader(oAuthRequest);
}
Also used : OAuthRequest(com.funstill.kelefun.http.OAuthRequest) HttpUrl(okhttp3.HttpUrl)

Aggregations

OAuthRequest (com.funstill.kelefun.http.OAuthRequest)2 HttpUrl (okhttp3.HttpUrl)2 FormBody (okhttp3.FormBody)1 RequestBody (okhttp3.RequestBody)1