【问题标题】:Get OAuth Token from Web API to android从 Web API 获取 OAuth 令牌到 android
【发布时间】:2016-11-15 10:40:44
【问题描述】:

我正在尝试从我的 web api 获取 ِِOAuth 令牌。我已经在 Postman 中测试了 api,它运行良好。

Screenshot for the successful call from Postman

现在我需要使用改造 2 从我的 android 应用程序调用此 Api 以获取令牌,因为我尝试使用以下接口但它不起作用。

@FormUrlEncoded
@POST("/Token")
public Call<String> Token(@Field("grant_type") String grant_type, @Field("username") String username, @Field("password") String password);

我怎样才能完成这项工作?

【问题讨论】:

    标签: android oauth asp.net-web-api2 retrofit2


    【解决方案1】:

    在我的情况下,我使用以下方式在 Android 应用程序中调用 Web Api。

    注意:我使用的是 OkHttp 库。

    创建令牌类

    public class Token {
        public String access_token;
        public String token_type;
        public int expires_in;
    
        public Token(){
    
        }
    }
    

    现在将 OkHttp 库 jar 添加到您的项目中。

    更多详情请访问:

    http://square.github.io/okhttp/

    现在以这种方式调用 OAuth Token

                OkHttpClient client = new OkHttpClient();
                Request.Builder builder = new Request.Builder();
                builder.url(write your url here);
                builder.addHeader("Content-Type", "application/x-www-form-urlencoded");
                builder.addHeader("Accept", "application/json");
    
                FormEncodingBuilder parameters = new FormEncodingBuilder();
                parameters.add("grant_type", "password");
                parameters.add("username", edituser);
                parameters.add("password", editpassword);
                builder.post(parameters.build());
    
                try {
                    Response response = client.newCall(builder.build()).execute();
                    if (response.isSuccessful()) {
                        String json = response.body().string();
    
                    }
                catch(Exception e){
                  // catch Exception here
              }
    

    最后你会得到你的令牌。

    【讨论】:

    【解决方案2】:

    晚了,但它可能会帮助某人

    接口类

    @FormUrlEncoded
        @POST("/Token")
        void getTokenAccess(@Field("grant_type") String grantType, @Field("username") String username, @Field("password") String password, Callback<TokenResponse> callback);
    

    TokenResponse 类

    public class TokenResponse {
        private String access_token,token_type,expires_in,userName;
    
        public String getAccess_token() {
            return access_token;
        }
    
        public String getToken_type() {
            return token_type;
        }
    
        public String getExpires_in() {
            return expires_in;
        }
    
        public String getUserName() {
            return userName;
        }
    }
    

    我正在使用改造 1.9.0

    RestAdapter adapter = new RestAdapter.Builder()
                        .setEndpoint(ENDPOINT_URL).setLogLevel(RestAdapter.LogLevel.FULL)
                        .build();
                NetApi api = adapter.create(NetApi.class);
    
                api.getTokenAccess("password", email, password, new Callback<TokenResponse>() {
    
    
                    @Override
                    public void success(TokenResponse tokenResponse, Response response) {
                        showProgress(false);
                        try{
                            MainActivity.tokenResponse = tokenResponse;
                            startActivity(new Intent(LoginActivity.this, MainActivity.class));
                        }catch (Exception e){
                            Toast.makeText(LoginActivity.this,"Unknown Error",Toast.LENGTH_LONG).show();
                            e.printStackTrace();
                        }
                    }
    
                    @Override
                    public void failure(RetrofitError error) {
                        Toast.makeText(LoginActivity.this,""+error.getMessage(),Toast.LENGTH_LONG).show();
                        showProgress(false);
                    }
                });
    

    【讨论】:

      猜你喜欢
      • 2019-03-13
      • 2017-05-20
      • 2021-01-10
      • 2017-07-12
      • 1970-01-01
      • 2015-07-21
      • 1970-01-01
      • 2014-04-18
      • 1970-01-01
      相关资源
      最近更新 更多