栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

OAuth-无效令牌:不允许使用请求令牌

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

OAuth-无效令牌:不允许使用请求令牌

您使用的不是OAuth 2.0,而是使用HMAC-SHA1作为签名方法的OAuth 1.0。要使用OAuth 2.0,您至少需要gdata-java-
client
库的1.47.0版本和google-oauth-
java-client
库的1.8.0-beta版本。

使用google-api-java-client库可提供帮助程序类来处理Google的OAuth 2.0实现。

要检索OAuth 2.0凭据,您可以使用以下代码段:

import com.google.api.client.auth.oauth2.Credential;import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeRequestUrl;import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest;import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse;import com.google.api.client.http.HttpTransport;import com.google.api.client.http.javanet.NetHttpTransport;import com.google.api.client.json.jackson.JacksonFactory;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.Arrays;import java.util.List;public class MyClass {  // Retrieve the CLIENT_ID and CLIENT_SECRET from an APIs Console project:  //     https://pre.google.com/apis/console  static String CLIENT_ID = "<YOUR_CLIENT_ID>";  static String CLIENT_SECRET = "<YOUR_CLIENT_SECRET>";  // Change the REDIRECT_URI value to your registered redirect URI for web  // applications.  static String REDIRECT_URI = "urn:ietf:wg:oauth:2.0:oob";  // Add other requested scopes.  static List<String> SCOPES = Arrays.asList("https://docs.google.com/feeds");    static Credential getCredentials() throws IOException {    HttpTransport transport = new NetHttpTransport();    JacksonFactory jsonFactory = new JacksonFactory();    // Step 1: Authorize -->    String authorizationUrl =        new GoogleAuthorizationCodeRequestUrl(CLIENT_ID, REDIRECT_URI, SCOPES).build();    // Point or redirect your user to the authorizationUrl.    System.out.println("Go to the following link in your browser:");    System.out.println(authorizationUrl);    // Read the authorization pre from the standard input stream.    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));    System.out.println("What is the authorization pre?");    String pre = in.readLine();    // End of Step 1 <--    // Step 2: Exchange -->    GoogleTokenResponse response =        new GoogleAuthorizationCodeTokenRequest(transport, jsonFactory, CLIENT_ID, CLIENT_SECRET, pre, REDIRECT_URI).execute();    // End of Step 2 <--    // Build a new GoogleCredential instance and return it.    return new GoogleCredential.Builder().setClientSecrets(CLIENT_ID, CLIENT_SECRET)        .setJsonFactory(jsonFactory).setTransport(transport).build()        .setAccessToken(response.getAccessToken()).setRefreshToken(response.getRefreshToken());  }  // …}

拥有OAuth 2.0凭据后,您可以按以下方式授权服务对象:

// ...import com.google.api.client.auth.oauth2.Credential;import com.google.gdata.client.docs.DocsService;import com.google.gdata.data.docs.documentListEntry;import com.google.gdata.data.docs.documentListFeed;import com.google.gdata.util.ServiceException;// ...import java.io.IOException;import java.net.URL;// ...public class MyClass {  // …    static void printdocuments(Credential credential) throws IOException, ServiceException {    // Instantiate and authorize a new DocsService object.    DocsService service = new DocsService("<YOUR_APPLICATION_NAME>");    service.setOAuth2Credentials(credential);    // Send a request to the documents List API to retrieve document entries.    URL feedUri = new URL("https://docs.google.com/feeds/default/private/full/");    documentListFeed feed = service.getFeed(feedUri, documentListFeed.class);    for (documentListEntry entry : feed.getEntries()) {      System.out.println("Title: " + entry.getTitle().getPlainText());    }  }  // ...}

CLIENT_ID
CLIENT_SECRET
可以从检索API控制台和
REDIRECT_URI
必须匹配一个已注册的API项目。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/515428.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号