将 Retrofit 与 Imgur 的 API 结合使用

编程入门 行业动态 更新时间:2024-10-25 20:18:31
本文介绍了将 Retrofit 与 Imgur 的 API 结合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我试图将 Retrofit 库与 Imgur 的 API 一起使用,但没有成功.我不断收到 403 Permission Denied 错误.Imgur 用于我尝试做的事情的唯一授权是通过标头,我(相信)我做得对.我当前的代码如下:

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/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)

推荐答案

哈哈,傻我...

方法 getImage 有注释 @POST("/image/{id}") 而不是 @GET("/image/{id}")

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

这篇关于将 Retrofit 与 Imgur 的 API 结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-18 06:17:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/932680.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Retrofit   Imgur   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!