Exemplo n.º 1
0
  private boolean testAuth() {
    // Now let's go and ask for a protected resource!
    System.out.println("Testing authentication...");

    Response response = sendRequest(new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL));
    System.out.println(response.isSuccessful() ? "Success" : "Failure");
    return response.isSuccessful();
  }
Exemplo n.º 2
0
    @Override
    protected Boolean doInBackground(HashMap<String, String>... params) {
      SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getActivity());
      // Load the saved accessToken
      Token accessToken =
          new Token(
              settings.getString("accessToken", null), settings.getString("accessSecret", null));
      OAuthService service =
          new ServiceBuilder()
              .provider(OpenStreetMapAPI.class)
              .apiKey("3rTM4by36P1CsMfiwZ1g58L1c1tZOpQWLUo591tx")
              .apiSecret("Cl2NKl6LFAvKqXpI7Kvccwyamdz80rHAXLruWhbG")
              .build();
      Toast.makeText(m_a, m_a.getResources().getString(R.string.add_loading), Toast.LENGTH_LONG)
          .show();
      /** Getting a changeset from the api */
      OAuthRequest changeset_request =
          new OAuthRequest(Verb.PUT, "http://api.openstreetmap.org/api/0.6/changeset/create");
      String changeset_payload =
          "<osm>\n"
              + "<changeset>\n"
              + "<tag k=\"created_by\" v=\"TakeABreak 1.5\"/>\n"
              + "</changeset>\n"
              + "</osm>";
      changeset_request.addPayload(changeset_payload);
      changeset_request.addHeader("Content-Length", Integer.toString(changeset_payload.length()));
      changeset_request.addHeader("Content-Type", "text/xml");
      service.signRequest(accessToken, changeset_request); // the access token from step 4
      Response changeset_response = changeset_request.send();
      Log.d("TakeABreak", "Changeset: " + changeset_response.getBody());

      params[0].put("changeset", changeset_response.getBody());

      /** PUT-ing the change proper. */
      OAuthRequest request =
          new OAuthRequest(Verb.PUT, "http://api.openstreetmap.org/api/0.6/node/create");
      String payload = generatePayload(params[0]);
      request.addPayload(payload);
      request.addHeader("Content-Length", Integer.toString(payload.length()));
      request.addHeader("Content-Type", "text/xml");
      service.signRequest(accessToken, request); // the access token from step 4
      Response response = request.send();
      // Log.d("TakeABreak", response.toString());

      Iterator<Entry<String, String>> it = response.getHeaders().entrySet().iterator();
      Log.d("TakeABreak", response.getBody());
      // System.out.println(response.getBody());
      return response.isSuccessful();
      // return true;
    }