Add token to the header of Vimeo API Android

喜欢而已 提交于 2019-11-29 12:03:10

You can do this with version 1.0.1 of the vimeo-networking library. If you refer to the new section in the README as well as the sample app, it will cover how to initialize the VimeoClient instance with an oAuth Token (or "access token") provided by the developer console.

VimeoClient.initialize(new Configuration.Builder(<accessToken>).build());

You can alternately initialize the builder with the client id and client secret and then set the access token on the builder with setAccessToken(<accessToken>) if you want to also allow for code grant authentication.

Setting this access token on the builder will default all requests to use that token. If you've provide the client id and client secret, then users of your app could authenticate with a code grant authentication. This will overwrite your initially provided "access token" and all future requests will use their token.

Full disclosure: I'm one of the authors of this library. We also highly encourage any issues or feature requests to be filed here.

Use HttpURLConnection and pass OAUTH_TOKEN in the header

String oAuthToken = "your-token";
HttpURLConnection urlConnection = null;
URL vimeoURL = new URL("https://api.vimeo.com");
try {
    urlConnection = (HttpURLConnection) vimeoURL.openConnection();
    // set authentication
    String auth = "Bearer " + oAuthToken;
    urlConnection.setRequestProperty("Authorization", auth.trim());
    // set request method
    urlConnection.setRequestMethod("GET");
    if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
         // do something
    }
  } catch (Exception e) {// append e
        // do something
  } finally {
      if (urlConnection != null) {// close connection
         urlConnection.disconnect();
     }
  }    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!