热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

HandlingNullObjectEncodinginOAuth1.0aAPIImplementation

ExploreacommonissueencounteredwhenimplementinganOAuth1.0aAPI,specificallytheinabilitytoencodenullobjectsandhowtoresolveit.
Hi,

I've encountered a frequent problem while setting up a custom API that doesn't require an access key but is secured with OAuth 1.0a. The API requires signing requests using your client key and secret. Although there are several libraries available for handling authentication, I've run into issues with encoding null objects during the request token generation process.

Here's my current implementation:

```java
public class NounProjectApi extends DefaultApi10a {

private static final String AUTHORIZE_URL = "http://api.thenounproject.com/icon/1";

protected NounProjectApi() {}

private static class InstanceHolder {
private static final NounProjectApi INSTANCE = new NounProjectApi();
}

public static NounProjectApi instance() {
return InstanceHolder.INSTANCE;
}

public String getAccessTokenEndpoint() {
return "http://api.thenounproject.com/icon/1";
}

public String getRequestTokenEndpoint() {
return "http://api.thenounproject.com/icon/1";
}

public String getAuthorizationUrl(OAuth1RequestToken requestToken) {
return String.format(AUTHORIZE_URL, requestToken.getToken());
}
}
```

The code fails at `OAuth1RequestToken requestToken = service.getRequestToken();` with the following error:

```
Caused by: java.lang.IllegalArgumentException: Cannot encode null object
at com.github.scribejava.core.utils.Preconditions.check(Preconditions.java:49)
at com.github.scribejava.core.utils.Preconditions.checkNotNull(Preconditions.java:19)
at com.github.scribejava.core.utils.OAuthEncoder.encode(OAuthEncoder.java:26)
at com.github.scribejava.core.model.Parameter.asUrlEncodedPair(Parameter.java:16)
at com.github.scribejava.core.model.ParameterList.asFormUrlEncodedString(ParameterList.java:63)
at com.github.scribejava.core.model.ParameterList.asOauthBaseString(ParameterList.java:53)
at com.github.scribejava.core.extractors.BaseStringExtractorImpl.getSortedAndEncodedParams(BaseStringExtractorImpl.java:41)
at com.github.scribejava.core.extractors.BaseStringExtractorImpl.extract(BaseStringExtractorImpl.java:24)
at com.github.scribejava.core.oauth.OAuth10aService.getSignature(OAuth10aService.java:169)
at com.github.scribejava.core.oauth.OAuth10aService.addOAuthParams(OAuth10aService.java:88)
at com.github.scribejava.core.oauth.OAuth10aService.prepareRequestTokenRequest(OAuth10aService.java:72)
at com.github.scribejava.core.oauth.OAuth10aService.getRequestToken(OAuth10aService.java:39)
at co.beek.pano.service.restService.SocialRestController.getNounProjectIcon(SocialRestController.java:71)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
```

To address this issue, ensure that all parameters passed to the OAuth encoder are not null. You can add null checks or provide default values where necessary. Additionally, consider using a more robust library like Signpost, which simplified this process significantly for me.

Thanks!
推荐阅读
author-avatar
风中凌乱2602938623
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有