我想使用LinkedIn android SDK分享信息到LinkedIn。以前,它工作正常。但是从过去几天开始,我突然收到“未设置访问令牌” 的异常。
我用于登录的代码:
public void login(final OnAuthorization onAuthorization) {
if (context instanceof Activity) {
Scope scope = Scope.build(Scope.R_BASICPROFILE, Scope.R_EMAILADDRESS, Scope.W_SHARE, Scope.RW_COMPANY_ADMIN);
LISessionManager.getInstance(context).init((Activity) context, scope//pass the build scope here
, new AuthListener() {
@Override
public void onAuthSuccess() {
onAuthorization.onAuthSuccess();
}
@Override
public void onAuthError(LIAuthError error) {
// Handle authentication errors
onAuthorization.onAuthFailure(error);
Log.e("linkedin error ", "Auth Error :" + error.toString());
}
}, true);//if TRUE then it will show dialog if
// any device has no LinkedIn app installed to download app else won't show anything
}
}
此处OnAuthorization
是通知LinkedIn成功与失败的界面。
public interface OnAuthorization {
void onAuthSuccess();
void onAuthFailure(LIAuthError liAuthError);
}
成功通过LinkedIn身份验证(登录)。共享完成。
public void share(BlogInfo blogInfo, final OnSharingComplete onSharingComplete) {
String url = "https://api.linkedin.com/v1/people/~/shares";
Visibility visibility = new Visibility(Visibility.CODE_ANYONE);
Content content = new Content(blogInfo.getTitle(), blogInfo.getContent(), blogInfo.getBlogUrl(), blogInfo.getImageUrl());
LinkedInShareRequestModel linkedInShareRequestModel = new LinkedInShareRequestModel(blogInfo.getDate(), visibility, content);
Log.e("gson", new Gson().toJson(linkedInShareRequestModel));
APIHelper apiHelper = APIHelper.getInstance(context);
apiHelper.postRequest(context, url, new Gson().toJson(linkedInShareRequestModel), new ApiListener() {
@Override
public void onApiSuccess(ApiResponse apiResponse) {
// Success!
Log.d("", "Share response : " + apiResponse.toString());
onSharingComplete.onComplete(true, null);
logout();
}
@Override
public void onApiError(LIApiError liApiError) {
// Error making POST request!
Log.e("", "Share error : " + liApiError.getLocalizedMessage());
onSharingComplete.onComplete(false, liApiError);
}
});
}
在这里,BlogInfo
是一个如下模型
public class BlogInfo implements Parcelable {
public static final Creator<BlogInfo> CREATOR = new Creator<BlogInfo>() {
@Override
public BlogInfo createFromParcel(Parcel in) {
return new BlogInfo(in);
}
@Override
public BlogInfo[] newArray(int size) {
return new BlogInfo[size];
}
};
private int id;
private String title;
private String subTitle;
private String content;
private String date;
private String imageUrl;
private String blogUrl;
public BlogInfo(int id, String title, String subTitle, String content, String date, String imageUrl, String blogUrl) {
this.id = id;
this.title = title;
this.subTitle = subTitle;
this.content = content;
this.date = date;
this.imageUrl = imageUrl;
this.blogUrl = blogUrl;
}
public BlogInfo(String title, String subTitle, String content, String date, String imageUrl, String blogUrl) {
this.title = title;
this.subTitle = subTitle;
this.content = content;
this.date = date;
this.imageUrl = imageUrl;
this.blogUrl = blogUrl;
}
private BlogInfo(Parcel in) {
id = in.readInt();
title = in.readString();
subTitle = in.readString();
content = in.readString();
date = in.readString();
imageUrl = in.readString();
blogUrl = in.readString();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getSubTitle() {
return subTitle;
}
public void setSubTitle(String subTitle) {
this.subTitle = subTitle;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
public String getBlogUrl() {
return blogUrl;
}
public void setBlogUrl(String blogUrl) {
this.blogUrl = blogUrl;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int flags) {
parcel.writeInt(id);
parcel.writeString(title);
parcel.writeString(subTitle);
parcel.writeString(content);
parcel.writeString(date);
parcel.writeString(imageUrl);
parcel.writeString(blogUrl);
}
}
和OnSharingComplete
还是一个界面,用于通知共享是成功完成还是失败。
public interface OnSharingComplete {
void onComplete(boolean isSuccessful,Exception exception);
}
有人可以帮我解决这个错误吗??