/**
   * Authorises, initialises and returns a new FreeAgentClient instance.
   *
   * @param identifier The identifier to use for OAuth authentication and FreeAgentService
   *     recognition.
   * @param secret The secret to use for OAuth authentication.
   * @param apiURL The URL of the API to target {@link #LIVE_URL}, {@link #SANDBOX_URL} etc.
   * @param loggingEnabled Should logging be enabled.
   * @return An authenticated instance which is ready to use or null
   * @throws IOException Thrown a problem is encountered during the OAuth process.
   */
  public static FreeAgentClient authorise(
      String identifier, String secret, String apiURL, boolean loggingEnabled) throws IOException {

    if (DATA_STORE_FACTORY == null) {
      DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR);
    }

    AuthorizationCodeFlow flow =
        new AuthorizationCodeFlow.Builder(
                BearerToken.authorizationHeaderAccessMethod(),
                HTTP_TRANSPORT,
                JSON_FACTORY,
                new GenericUrl(apiURL + "/token_endpoint"),
                new ClientParametersAuthentication(identifier, secret),
                identifier,
                apiURL + "/approve_app")
            .setDataStoreFactory(DATA_STORE_FACTORY)
            .build();

    // authorize
    LocalServerReceiver receiver =
        new LocalServerReceiver.Builder().setHost(LOCALHOST).setPort(8080).build();

    final Credential credential =
        new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");

    if (credential != null) {
      return new FreeAgentClient(credential, apiURL, loggingEnabled);
    } else {
      return null;
    }
  }
Exemplo n.º 2
0
 @Before
 public void setUp() {
   mockHttpTransport = new MockHttpTransport();
   credential =
       new Credential.Builder(BearerToken.authorizationHeaderAccessMethod())
           .setTransport(mockHttpTransport)
           .setJsonFactory(new JacksonFactory())
           .setClientAuthentication(
               new ClientParametersAuthentication("CLIENT_ID", "CLIENT_SECRET"))
           .setTokenServerUrl(TOKEN_SERVER_URL)
           .build();
   oAuth2Helper = new OAuth2Helper();
 }
Exemplo n.º 3
0
  /**
   * Searches a track title in a spreadsheet.
   *
   * @param trackName the track name to search
   * @param activity to get context
   * @param spreadsheetTitle the title of spreadsheet
   * @param isDelete whether delete the information of this track in the document
   * @param accountName the name of Google account
   * @return true means find the track name in the spreadsheet
   */
  private static boolean searchTrackTitleInSpreadsheet(
      String trackName,
      Activity activity,
      String spreadsheetTitle,
      boolean isDelete,
      String accountName) {
    try {
      // Get spreadsheet Id.
      String spreadsheetId =
          searchAllSpreadsheetByTitle(spreadsheetTitle, activity, accountName).get(0).getId();

      // Get spreadsheet service.
      SpreadsheetService spreadsheetService = new SpreadsheetService(spreadsheetTitle);
      Credential credential = new Credential(BearerToken.authorizationHeaderAccessMethod());
      credential.setAccessToken(
          SendToGoogleUtils.getToken(
              activity.getApplicationContext(), accountName, SendToGoogleUtils.SPREADSHEETS_SCOPE));
      spreadsheetService.setOAuth2Credentials(credential);

      // Get work sheet.
      WorksheetFeed feed =
          spreadsheetService.getFeed(
              new URL(
                  String.format(
                      Locale.US, SendSpreadsheetsAsyncTask.GET_WORKSHEETS_URI, spreadsheetId)),
              WorksheetFeed.class);
      List<WorksheetEntry> data = feed.getEntries();
      for (Iterator<WorksheetEntry> iterator = data.iterator(); iterator.hasNext(); ) {
        WorksheetEntry worksheetEntry = (WorksheetEntry) iterator.next();
        String title = worksheetEntry.getTitle().getPlainText();
        if (title.equals(WORK_SHEET_NAME)) {
          URL listFeedUrl = worksheetEntry.getListFeedUrl();
          List<ListEntry> listFeed =
              spreadsheetService.getFeed(listFeedUrl, ListFeed.class).getEntries();
          for (Iterator<ListEntry> iterator2 = listFeed.iterator(); iterator2.hasNext(); ) {
            ListEntry listEntry = (ListEntry) iterator2.next();
            String name = listEntry.getCustomElements().getValue(TRANCK_NAME_COLUMN);
            if (name.equals(trackName)) {
              if (isDelete) {
                listEntry.delete();
              }
              return true;
            }
          }
        }
      }
    } catch (Exception e) {
      Log.e(EndToEndTestUtils.LOG_TAG, "Search spreadsheet failed.");
    }
    return false;
  }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
      super.onActivityCreated(savedInstanceState);
      // setup credential store
      SharedPreferencesCredentialStore credentialStore =
          new SharedPreferencesCredentialStore(
              getActivity(), SamplesConstants.CREDENTIALS_STORE_PREF_FILE, OAuth.JSON_FACTORY);
      // setup authorization flow
      AuthorizationFlow flow =
          new AuthorizationFlow.Builder(
                  BearerToken.authorizationHeaderAccessMethod(),
                  OAuth.HTTP_TRANSPORT,
                  OAuth.JSON_FACTORY,
                  new GenericUrl(FoursquareConstants.TOKEN_SERVER_URL),
                  new ClientParametersAuthentication(
                      FoursquareConstants.CLIENT_ID, FoursquareConstants.CLIENT_SECRET),
                  FoursquareConstants.CLIENT_ID,
                  FoursquareConstants.AUTHORIZATION_CODE_SERVER_URL)
              .setScopes(Lists.<String>newArrayList())
              .setCredentialStore(credentialStore)
              .build();
      // setup UI controller
      AuthorizationDialogController controller =
          new DialogFragmentController(getFragmentManager()) {
            @Override
            public String getRedirectUri() throws IOException {
              return FoursquareConstants.REDIRECT_URL;
            }

            @Override
            public boolean isJavascriptEnabledForWebView() {
              return true;
            }
          };
      // instantiate an OAuthManager instance
      oauth = new OAuthManager(flow, controller);
    }
 @Before
 public void setUp() throws Exception {
   credential = new Credential.Builder(BearerToken.authorizationHeaderAccessMethod()).build();
 }