Using Retrofit with Imgur's API

混江龙づ霸主 提交于 2019-12-13 01:46:54

问题


I'm attempting to use the Retrofit library with Imgur's API with no success. I keep receiving 403 Permission Denied errors. The only authorization Imgur uses for what I'm attempting to do is through a header, which I (believe) I am doing correctly. My current code is the following:


package me.rabrg.imgur;

import me.rabrg.imgur.response.Image;
import me.rabrg.imgur.service.ImageService;
import retrofit.RequestInterceptor;
import retrofit.RestAdapter;

public class ImgurApi {

    private final RestAdapter restAdapter;
    private final ImageService imageService;

    public ImgurApi(final String clientId) {
        this.restAdapter = new RestAdapter.Builder().setEndpoint("https://api.imgur.com/3").setRequestInterceptor(new RequestInterceptor() {
            @Override
            public void intercept(final RequestFacade request) {
                request.addHeader("Authorization", "Client-ID " + clientId);
            }
        }).build();

        this.imageService = restAdapter.create(ImageService.class);
    }

    public Image getImage(final String id) {
        return imageService.getImage(id);
    }
}

package me.rabrg.imgur.service;

import me.rabrg.imgur.response.Image;
import retrofit.http.POST;
import retrofit.http.Path;

public interface ImageService {

    @POST("/image/{id}")
    Image getImage(@Path("id") String id);
}

new ImgurApi(clientId).getImage(id)

回答1:


Haha, silly me...

The method getImage had the annotation @POST("/image/{id}") instead of @GET("/image/{id}")



来源:https://stackoverflow.com/questions/28800515/using-retrofit-with-imgurs-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!