Ejemplo n.º 1
0
  public String postFile(URLFetchService us, List<PostObj> postobjlist) throws Exception {
    int index;

    Gson gson;
    String linkID;
    List<String> linkList;
    HTTPRequest req;
    String param;

    gson = new Gson();

    linkID = createUID();
    linkList = new ArrayList<String>();
    for (index = 0; index < postobjlist.size(); index++) {
      linkList.add(postobjlist.get(index).filelink);
    }

    param =
        URLEncoder.encode("postlist", "UTF-8")
            + "="
            + URLEncoder.encode(gson.toJson(postobjlist), "UTF-8")
            + '&'
            + URLEncoder.encode("linkid", "UTF-8")
            + "="
            + URLEncoder.encode(linkID, "UTF-8")
            + '&'
            + URLEncoder.encode("linklist", "UTF-8")
            + "="
            + URLEncoder.encode(gson.toJson(linkList), "UTF-8");
    req =
        new HTTPRequest(
            new URL("http://tnfshmoe.appspot.com/postdf89ksfxsyx9sfdex09usdjksd"), HTTPMethod.POST);
    req.setPayload(param.getBytes());
    us.fetch(req);

    return linkID;
  }
Ejemplo n.º 2
0
  public UserId determineUserId(String code) throws IOException {
    Map<String, String> parameters = Maps.newHashMap();
    parameters.put("code", code);
    parameters.put("client_id", clientId);
    parameters.put("client_secret", clientSecret);
    parameters.put("redirect_uri", uriBuilder.forPath(OAuthCallbackServlet.PATH).toString());
    parameters.put("grant_type", "authorization_code");

    HTTPRequest fetchRequest =
        new HTTPRequest(new URL("https://accounts.google.com/o/oauth2/token"), HTTPMethod.POST);
    fetchRequest.setPayload(buildKeyValueString(parameters, true).getBytes());
    HTTPResponse response = urlFetchService.fetch(fetchRequest);
    JsonObject object = jsonParser.parse(new String(response.getContent())).getAsJsonObject();
    String access_token = object.get("access_token").getAsString();

    HTTPRequest secondRequest =
        new HTTPRequest(new URL("https://www.googleapis.com/oauth2/v1/userinfo"));
    secondRequest.addHeader(
        new HTTPHeader("Authorization", String.format("Bearer %s", access_token)));
    response = urlFetchService.fetch(secondRequest);
    object = jsonParser.parse(new String(response.getContent())).getAsJsonObject();

    return UserId.fromString(object.get("id").getAsString());
  }