在我的代码中得到以下错误。
400错误请求{ "error“:"redirect_uri_mismatch”}
我不明白哪里出了问题。我似乎正确地使用了他们的规范中定义的google api。在开发人员控制台中创建已安装的应用程序后,我从浏览器获得了授权码,并将其插入。redirect_uri是从控制台中选择的。有人能告诉我redirect_uri出了什么问题吗?我还不能找出这个参数的错误所在。
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest;
import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.store.DataStoreFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.adexchangeseller.AdExchangeSeller;
import com.google.api.services.adexchangeseller.AdExchangeSellerScopes;
import com.google.api.services.adexchangeseller.model.AdClients;
import com.google.api.services.adexchangeseller.model.AdUnits;
import com.google.api.services.adexchangeseller.model.CustomChannels;
import com.google.api.services.adexchangeseller.model.SavedReports;
import com.google.api.services.adexchangeseller.AdExchangeSeller;
import com.google.api.services.adexchangeseller.AdExchangeSeller.Reports.Generate;
import com.google.api.services.adexchangeseller.model.Report;
import java.io.FileInputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
public class Reporting {
public class AdXReporting {
private static String AD_CLIENT_ID = "....";
private static final String APPLICATION_NAME = "AdX Installed app product";
private static final String authorizationCode = "..............";
private static final String CALLBACK_URL = "urn:ietf:wg:oauth:2.0:oob";
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static final java.io.File DATA_STORE_DIR = new java.io.File("adexchangeseller_sample");
private static void authorize() {
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
new InputStreamReader(new FileInputStream("client_secrets.json")));
GoogleAuthorizationCodeFlow flow =
new GoogleAuthorizationCodeFlow.Builder(
httpTransport,
JSON_FACTORY,
clientSecrets,
Collections.singleton(AdExchangeSellerScopes.ADEXCHANGE_SELLER_READONLY)
).setDataStoreFactory(dataStoreFactory).build();
GoogleAuthorizationCodeTokenRequest tokenRequest =
flow.newTokenRequest(authorizationCode);
tokenRequest.setRedirectUri(CALLBACK_URL);
GoogleTokenResponse tokenResponse = tokenRequest.execute();
// Store the credential for the user.
flow.createAndStoreCredential(tokenResponse, AD_CLIENT_ID);
}
}发布于 2014-08-06 22:30:58
你的指针是正确的。
我为哪里出了问题苦思冥想了两天。
显然,redirect_uri需要在身份验证和令牌请求之间匹配。
如果不是,redirect_uri_mismatch将被抛出。
google api的文档说要从开发人员控制台使用redirect_uri。
但是,它们自动生成并粘贴到浏览器中的url并不会这样做,而是使用不同的localhost +端口url。
这是问题的根本来源。
如果你使用他们要求你在浏览器中剪切和粘贴的身份验证url,并从控制台插入正确的redirect_uri "urn:ietf:wg:oauth:2.0:oob“并获取代码,然后使用相同的redirect_uri来获取令牌,那么就没有redirect_uri_mismatch了。
谢谢你的回答。
发布于 2014-08-06 01:10:19
不,我没有提供一个redirect_uri来获取授权码,但它是由谷歌代码预先填充的一些https localhost uri,我用它来获取授权码。它说,在浏览器中剪切并粘贴url以获得授权码。
我为access_token请求使用的redirect_uri是不同的值,并且是我从控制台"urn:ietf:wg:oauth:2.0:oob“中剪切并粘贴的,并且我在控制台设置中有一个由谷歌预先设置的普通本地主机,也用于已安装应用程序/其他项目的redirect_uri,但我在本地主机上没有https web服务器。
对于授权码和访问令牌请求,redirect_uri是否应该匹配?如果是这样,安装的应用程序/其他应该是什么。我应该使用"urn:ietf:wg:oauth:2.0:oob“作为redirect_uri来获取授权码吗?
https://stackoverflow.com/questions/25129281
复制相似问题